User.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use app\common\library\Sms;
  5. use fast\Random;
  6. use GuzzleHttp\Client;
  7. use think\Config;
  8. use think\Exception;
  9. use think\Validate;
  10. use think\Db;
  11. /**
  12. * 会员接口
  13. */
  14. class User extends Apic
  15. {
  16. protected $noNeedLogin = ['accountlogin','resetpwd'];
  17. protected $noNeedRight = '*';
  18. //员工手机+密码登录
  19. public function accountlogin(){
  20. $mobile = $this->request->post('mobile');
  21. $password = $this->request->post('password');
  22. if (!$mobile || !$password) {
  23. $this->error(__('Invalid parameters'));
  24. }
  25. $ret = $this->auth->login($mobile, $password);
  26. if ($ret) {
  27. $data = $this->auth->getUserinfo();
  28. $this->success(__('Logged in successful'), $data);
  29. } else {
  30. $this->error($this->auth->getError());
  31. }
  32. }
  33. /**
  34. * 退出登录
  35. * @ApiMethod (POST)
  36. */
  37. public function logout()
  38. {
  39. if (!$this->request->isPost()) {
  40. $this->error(__('Invalid parameters'));
  41. }
  42. $this->auth->logout();
  43. $this->success(__('Logout successful'));
  44. }
  45. //用户详细资料
  46. public function getUserinfo($type = 1){
  47. $info = $this->auth->getUserinfo();
  48. if($type == 'return'){
  49. return $info;
  50. }
  51. $this->success(__('success'),$info);
  52. }
  53. /**
  54. * 重置密码
  55. *
  56. * @ApiMethod (POST)
  57. * @param string $mobile 手机号
  58. * @param string $captcha 验证码
  59. * @param string $newpassword 新密码
  60. */
  61. public function resetpwd()
  62. {
  63. $mobile = $this->request->post('mobile');
  64. $captcha = $this->request->post('captcha');
  65. $newpassword = $this->request->post("newpassword");
  66. if (!$mobile || !$captcha || !$newpassword) {
  67. $this->error(__('Invalid parameters'));
  68. }
  69. //验证Token
  70. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  71. $this->error(__('Password must be 6 to 30 characters'));
  72. }
  73. if (!Validate::regex($mobile, "^1\d{10}$")) {
  74. $this->error(__('Mobile is incorrect'));
  75. }
  76. $user = \app\common\model\CompanyStaff::getByMobile($mobile);
  77. if (!$user) {
  78. $this->error(__('User not found'));
  79. }
  80. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  81. if (!$ret) {
  82. $this->error(__('Captcha is incorrect'));
  83. }
  84. Sms::flush($mobile, 'resetpwd');
  85. //模拟一次登录
  86. $this->auth->direct($user->id);
  87. $ret = $this->auth->resetpwd($newpassword, '', true);
  88. if ($ret) {
  89. $this->success(__('Reset password successful'));
  90. } else {
  91. $this->error($this->auth->getError());
  92. }
  93. }
  94. /**
  95. * 修改会员个人信息
  96. *
  97. * @ApiMethod (POST)
  98. * @param string $avatar 头像地址
  99. * @param string $username 用户名
  100. * @param string $nickname 昵称
  101. * @param string $bio 个人简介
  102. */
  103. public function profile()
  104. {
  105. $field = [
  106. 'mobile',
  107. 'image',
  108. 'is_open',
  109. 'open_hours',
  110. ];
  111. $data = request_post_hub($field);
  112. $data['updatetime'] = time();
  113. $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
  114. $this->success('资料更新完成');
  115. }
  116. /**
  117. * 设置店铺地址
  118. */
  119. public function setaddress()
  120. {
  121. $field = [
  122. 'province_name',
  123. 'city_name',
  124. 'area_name',
  125. 'province_id',
  126. 'city_id',
  127. 'area_id',
  128. 'address',
  129. ];
  130. $data = request_post_hub($field);
  131. $data['full_address'] = $data['province_name'].$data['city_name'].$data['area_name'].$data['address'];
  132. $data['updatetime'] = time();
  133. $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
  134. $this->success('资料更新完成');
  135. }
  136. /**
  137. * 小程序码
  138. * @return void
  139. */
  140. public function getMiniCode()
  141. {
  142. try {
  143. $companyId = $this->auth->company_id;
  144. $companyWhere['id'] = $companyId;
  145. $companyWhere['status'] = 1;
  146. $company = Db::name('company')->where($companyWhere)->find();
  147. if (empty($company)) {
  148. throw new Exception('未找到门店信息');
  149. }
  150. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  151. if (empty($company['mini_code'])) {
  152. $client = new Client();
  153. $tk = getAccessToken();
  154. $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
  155. 'json' => [
  156. //'page' => 'pages/home/index',
  157. 'env_version'=>'trial',
  158. 'scene' => 'shopId='.$companyId,
  159. ]
  160. ]);
  161. $fileName = md5($companyId);
  162. $fileUrl = '/uploads/company/'.$fileName.'.png';
  163. $code = $res2->getBody()->getContents();
  164. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  165. $companyData['mini_code'] = $fileUrl;
  166. $companyRes = Db::name('company')->where($companyWhere)->update($companyData);
  167. if (!$companyRes) {
  168. throw new Exception('更新门店信息失败');
  169. }
  170. $miniCode = $httpStr.$fileUrl;
  171. } else {
  172. $miniCode = $httpStr.$company['mini_code'];
  173. }
  174. $result = [
  175. 'mini_code' => $miniCode,
  176. 'company_name' => $this->auth->company->name,
  177. 'company_image' => one_domain_image($this->auth->company->image),
  178. ];
  179. $this->success('获取成功',$result);
  180. } catch (Exception $e) {
  181. $this->error($e->getMessage());
  182. }
  183. }
  184. }