Пример #1
0
 /**
  * @新增企业号 数据入库
  */
 public function registerEnterprise($data = null, $Ip = null)
 {
     $data['user_id'] = 0;
     $data['user_type'] = count($data['user_type']) > 1 ? 4 : $data['user_type'][0];
     $data['user_addtime'] = time();
     $data['user_lastlogin'] = time();
     $data['user_lastip'] = $Ip;
     $data['user_status'] = 1;
     $model = new User();
     $model->attributes = $data;
     if (!is_null($this->onlyPhone($data['user_phone']))) {
         return $this->result(3, '手机号已存在');
     }
     if (!is_null($this->onlyEnterprise($data['mer_name']))) {
         return $this->result(4, '企业名称已存在');
     }
     if ($model->validate()) {
         $transaction = \Yii::$app->db->beginTransaction();
         try {
             $model->setAttributes($data);
             $model->user_password = md5($data['user_password']);
             $model->save(false);
             $user_id = $model->attributes['user_id'];
             $mer = new MerBase();
             $mer->mer_id = $user_id;
             $mer->mer_name = $data['mer_name'];
             $mer->mer_phone = $data['user_phone'];
             $mer->register_time = time();
             $mer->save(false);
             $transaction->commit();
             //提交事务会真正的执行数据库操作
             return $this->result(1, '注册成功');
         } catch (Exception $e) {
             $transaction->rollback();
             //如果操作失败, 数据回滚
             return $this->result(2, '注册失败');
         }
     } else {
         // 验证失败:$errors 是一个包含错误信息的数组
         $errors = $model->errors;
         if (count($errors) > 1) {
             return $this->result(5, '非法注册,请通过正确途径注册');
         } else {
             $val = array_values($errors);
             return $this->result(6, $val[0][0]);
         }
     }
 }