User.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. //验证
  106. if($this->auth->type != 1){
  107. $this->error('只有门店老板才能设置');
  108. }
  109. $field = [
  110. 'mobile',
  111. 'image',
  112. 'is_open',
  113. 'open_hours',
  114. ];
  115. $data = request_post_hub($field);
  116. $data['updatetime'] = time();
  117. $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
  118. $this->success('资料更新完成');
  119. }
  120. /**
  121. * 设置店铺地址
  122. */
  123. public function setaddress()
  124. {
  125. //验证
  126. if($this->auth->type != 1){
  127. $this->error('只有门店老板才能设置');
  128. }
  129. $field = [
  130. 'province_name',
  131. 'city_name',
  132. 'area_name',
  133. 'province_id',
  134. 'city_id',
  135. 'area_id',
  136. 'address',
  137. ];
  138. $data = request_post_hub($field);
  139. $data['full_address'] = $data['province_name'].$data['city_name'].$data['area_name'].$data['address'];
  140. $data['updatetime'] = time();
  141. $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
  142. $this->success('资料更新完成');
  143. }
  144. /**
  145. * 小程序码
  146. * @return void
  147. */
  148. public function getMiniCode()
  149. {
  150. try {
  151. $companyId = $this->auth->company_id;
  152. $companyWhere['id'] = $companyId;
  153. $companyWhere['status'] = 1;
  154. $company = Db::name('company')->where($companyWhere)->find();
  155. if (empty($company)) {
  156. throw new Exception('未找到门店信息');
  157. }
  158. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  159. if (empty($company['mini_code'])) {
  160. $client = new Client();
  161. $tk = getAccessToken();
  162. $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
  163. 'json' => [
  164. //'page' => 'pages/home/index',
  165. 'env_version'=>'trial',
  166. 'scene' => 'shopId='.$companyId,
  167. ]
  168. ]);
  169. $fileName = md5($companyId);
  170. $fileUrl = '/uploads/company/'.$fileName.'.png';
  171. $code = $res2->getBody()->getContents();
  172. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  173. $companyData['mini_code'] = $fileUrl;
  174. $companyRes = Db::name('company')->where($companyWhere)->update($companyData);
  175. if (!$companyRes) {
  176. throw new Exception('更新门店信息失败');
  177. }
  178. $miniCode = $httpStr.$fileUrl;
  179. } else {
  180. $miniCode = $httpStr.$company['mini_code'];
  181. }
  182. $result = [
  183. 'mini_code' => $miniCode,
  184. 'company_name' => $this->auth->company->name,
  185. 'company_image' => one_domain_image($this->auth->company->image),
  186. ];
  187. $this->success('获取成功',$result);
  188. } catch (Exception $e) {
  189. $this->error($e->getMessage());
  190. }
  191. }
  192. }