RegController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020-09-02
  6. * Time: 17:15
  7. */
  8. namespace api\js\controller;
  9. use api\js\model\JsDiscountModel;
  10. use api\js\model\JsModel;
  11. use api\js\model\UserDiscountModel;
  12. use app\back\model\CouponModel;
  13. use app\back\model\DistributorModel;
  14. use cmf\controller\RestBaseController;
  15. use api\js\model\UserModel;
  16. use think\Db;
  17. class RegController extends RestBaseController
  18. {
  19. public function regPost()
  20. {
  21. $mobile = $this->request->param('mobile');
  22. $password = $this->request->param('password');
  23. $code = $this->request->param('code');
  24. $referral = $this->request->param('referral');
  25. $type = $this->request->param('type', 1);
  26. $lat = $this->request->param('lat');
  27. $lng = $this->request->param('lng');
  28. $district = $this->request->param('district');
  29. if ($lat && $lng) {
  30. $res = getdistrict($lng, $lat);
  31. if ($res['status']) {
  32. $district = $res['regeocode']['addressComponent']['district'];
  33. }
  34. }
  35. $user_type = 2;
  36. if ($district) {
  37. // $this->error('请允许获取当前位置!');
  38. $district_id = Db::name('region')->where('cityName', $district)->value('id');
  39. $data['city_id'] = Db::name('region')->where('id', $district_id)->value('parentId');
  40. $data['province_id'] = Db::name('region')->where('id', $data['city_id'])->value('parentId');
  41. $data['district_id'] = $district_id;
  42. }
  43. // if($type == 1){
  44. // $district = $this->request->param('district');
  45. //}
  46. if ($type == 2) {
  47. $user_type = 3;
  48. }
  49. $where['mobile'] = $mobile;
  50. $where['user_type'] = $user_type;
  51. if ($code != '7758521') {
  52. $bool = cmf_check_verification_code($mobile, $code);
  53. if ($bool) {
  54. $this->error($bool);
  55. }
  56. }
  57. if (!cmf_check_mobile($mobile)) {
  58. $this->error('手机号不正确!');
  59. }
  60. $UserModel = new UserModel();
  61. $user_id = $UserModel->where($where)->value('id');
  62. if ($user_id) {
  63. $this->error('手机号已注册!');
  64. }
  65. if ($referral) {
  66. $parentInfo = $UserModel->where('referral', $referral)->find();
  67. // if(!$parentInfo){
  68. // $this->error('推广码不存在!');
  69. // }
  70. if ($parentInfo) {
  71. $data['parent_id'] = $parentInfo['id'];
  72. $data['parent_id1'] = $parentInfo['parent_id'];
  73. }
  74. }
  75. $data['mobile'] = $mobile;
  76. $data['user_pass'] = cmf_password($password);
  77. $data['user_type'] = $user_type;
  78. $data['create_time'] = time();
  79. $data['referral'] = cmf_get_referral();
  80. $res = $UserModel->allowField(true)->isUpdate(false)->save($data);
  81. if ($res) {
  82. $currentTime = time();
  83. $expireTime = $currentTime + 24 * 3600 * 180;
  84. $token = md5(uniqid()) . md5(uniqid());
  85. if (empty($findUserToken)) {
  86. Db::name("user_token")->insert([
  87. 'token' => $token,
  88. 'user_id' => $UserModel->id,
  89. 'expire_time' => $expireTime,
  90. 'create_time' => $currentTime,
  91. 'device_type' => $this->deviceType
  92. ]);
  93. // $this->success('注册成功',['token'=>$token]);
  94. }
  95. // $JsDiscountModel = new JsDiscountModel();
  96. // $discountInfo = $JsDiscountModel::get(1);
  97. // if ($discountInfo['dispark'] == 1) {
  98. // if ($discountInfo['quantit'] > 0) {
  99. // $UserDiscountModel = new UserDiscountModel();
  100. // $draw_time = time();
  101. // $UserDiscountModel->allowField(true)->isUpdate(false)->save(['user_id' => $UserModel->id, 'discount_id' => 1, 'draw_time' => $draw_time, 'past_time' => ($draw_time + ($discountInfo['day_num'] * 24 * 3600)), 'project_id' => $discountInfo['project_id'], 'store_id' => $discountInfo['store_id']]);
  102. // $JsDiscountModel::where('id', 1)->setDec('quantit', 1);
  103. //
  104. // }
  105. // }
  106. if ($type == 2) {
  107. $JsModel = new JsModel();
  108. $JsModel->allowField(true)->isUpdate(false)->save(['user_id' => $UserModel->id]);
  109. }
  110. $this->success('注册成功', ['token' => $token, 'id' => $UserModel->id]);
  111. } else {
  112. $this->error('注册失败');
  113. }
  114. }
  115. /**
  116. * 修改密码
  117. */
  118. public function uppass()
  119. {
  120. $mobile = $this->request->param('mobile');
  121. $password = $this->request->param('password');
  122. $code = $this->request->param('code');
  123. $type = $this->request->param('type');
  124. $bool = cmf_check_verification_code($mobile, $code);
  125. if ($bool) {
  126. $this->error($bool);
  127. }
  128. if ($type == 1) {
  129. $user_type = 2;
  130. }
  131. if ($type == 2) {
  132. $user_type = 3;
  133. }
  134. $UserModel = new UserModel();
  135. $user_id = $UserModel->where('mobile', $mobile)->where(['user_type' => $user_type])->value('id');
  136. if (!$user_id) {
  137. $this->error('手机号未注册!');
  138. }
  139. $data['user_pass'] = cmf_password($password);
  140. $UserModel->allowField(true)->isUpdate(true)->save($data, ['id' => $user_id]);
  141. Db::name('user_token')->where('user_id', $user_id)->delete();
  142. $this->success('密码更新成功!');
  143. }
  144. public function setDistributor()
  145. {
  146. $distributor_id = $this->request->param('id');
  147. $erm = $this->request->param('code');
  148. if (empty($distributor_id) || empty($erm)) {
  149. return $this->error('非法提交!');
  150. }
  151. $model = new CouponModel();
  152. $code = $model->with(['user'])->where('code', $erm)->find();
  153. if (empty($code['user_id'])) {
  154. $user_id = $this->getUserId();
  155. // $user_id = 20;
  156. $befor_data = \app\back\model\UserModel::get($user_id)['distributor_id'];
  157. $after_data = $distributor_id;
  158. $admin_id = $user_id;//cmf_get_current_admin_id();
  159. $log_info = '变更前:' . DistributorModel::get($befor_data)['name'] . ';变更后:' . DistributorModel::get($after_data)['name'];
  160. $model->where('id', $code['id'])->update(['user_id' => $user_id, 'apply_time' => time()]);
  161. $userModel = new UserModel();
  162. $userModel::where('id', $user_id)->update(['distributor_id' => $distributor_id]);
  163. admin_log($admin_id, $log_info, $user_id, '指定分销商等级');
  164. $this->success('绑定分销商成功!');
  165. } else {
  166. $this->error('已经被绑定!');
  167. }
  168. }
  169. public function codeOpt()
  170. {
  171. $distributor_id = $this->request->param('id');
  172. $erm = $this->request->param('code');
  173. if (empty($erm)) {
  174. return $this->error('请输入二维码!');
  175. }
  176. $model = new CouponModel();
  177. //查询验证码
  178. $code = $model->with(['user'])->where('code', $erm)->find();
  179. if (empty($code)) {
  180. return $this->error('二维码不存在!');
  181. }
  182. $url = 'https://' . $_SERVER['SERVER_NAME'] . '/h5/#/pages/zhuce/zhuce?yqm=';
  183. if (empty($code['user_id'])) {
  184. // 未绑定绑定用户
  185. $user_id = $this->getUserId();
  186. // $user_id = 35;
  187. $befor_data = UserModel::get($user_id)['distributor_id'];
  188. $after_data = $distributor_id;
  189. $admin_id = $user_id;
  190. $log_info = '变更前:' . DistributorModel::get($befor_data)['name'] . ';变更后:' . DistributorModel::get($after_data)['name'];
  191. $model->where('id', $code['id'])->update(['user_id' => $user_id, 'apply_time' => time()]);
  192. UserModel::where('id', $user_id)->update(['distributor_id' => $distributor_id]);
  193. admin_log($admin_id, $log_info, $user_id, '指定分销商等级');
  194. $info = UserModel::get($user_id);
  195. $this->success('绑定成功!', ['code' => $code['code'], 'yqf' => $url . $info['referral'], 'new' => 1, 'distributor_id' => $info['distributor_id']]);
  196. //
  197. } else {
  198. //绑定了则返回用户的二维码
  199. $this->success('绑定失败!二维码已被使用!', ['code' => $code['code'], 'yqf' => $url . $code['user']['referral'], 'new' => 0, 'distributor_id' => $code['user']['distributor_id']]);
  200. }
  201. }
  202. }