User.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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','getUserOpenid'];
  17. protected $noNeedRight = '*';
  18. //员工手机+密码登录
  19. public function accountlogin(){
  20. $mobile = $this->request->post('mobile');
  21. $password = $this->request->post('password');
  22. $openid = $this->request->post('openid','');
  23. if (!$mobile || !$password || !$openid) {
  24. $this->error(__('Invalid parameters'));
  25. }
  26. $ret = $this->auth->login($mobile, $password, $openid);
  27. if ($ret) {
  28. $data = $this->auth->getUserinfo();
  29. $this->success(__('Logged in successful'), $data);
  30. } else {
  31. $this->error($this->auth->getError());
  32. }
  33. }
  34. /**
  35. * 退出登录
  36. * @ApiMethod (POST)
  37. */
  38. public function logout()
  39. {
  40. if (!$this->request->isPost()) {
  41. $this->error(__('Invalid parameters'));
  42. }
  43. $this->auth->logout();
  44. $this->success(__('Logout successful'));
  45. }
  46. //用户详细资料
  47. public function getUserinfo($type = 1){
  48. $info = $this->auth->getUserinfo();
  49. if($type == 'return'){
  50. return $info;
  51. }
  52. $this->success(__('success'),$info);
  53. }
  54. /**
  55. * 重置密码
  56. *
  57. * @ApiMethod (POST)
  58. * @param string $mobile 手机号
  59. * @param string $captcha 验证码
  60. * @param string $newpassword 新密码
  61. */
  62. public function resetpwd()
  63. {
  64. $mobile = $this->request->post('mobile');
  65. $captcha = $this->request->post('captcha');
  66. $newpassword = $this->request->post("newpassword");
  67. if (!$mobile || !$captcha || !$newpassword) {
  68. $this->error(__('Invalid parameters'));
  69. }
  70. //验证Token
  71. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  72. $this->error(__('Password must be 6 to 30 characters'));
  73. }
  74. if (!Validate::regex($mobile, "^1\d{10}$")) {
  75. $this->error(__('Mobile is incorrect'));
  76. }
  77. $user = \app\common\model\CompanyStaff::getByMobile($mobile);
  78. if (!$user) {
  79. $this->error(__('User not found'));
  80. }
  81. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  82. if (!$ret) {
  83. $this->error(__('Captcha is incorrect'));
  84. }
  85. Sms::flush($mobile, 'resetpwd');
  86. //模拟一次登录
  87. $this->auth->direct($user->id);
  88. $ret = $this->auth->resetpwd($newpassword, '', true);
  89. if ($ret) {
  90. $this->success(__('Reset password successful'));
  91. } else {
  92. $this->error($this->auth->getError());
  93. }
  94. }
  95. /**
  96. * 修改会员个人信息
  97. *
  98. * @ApiMethod (POST)
  99. * @param string $avatar 头像地址
  100. * @param string $username 用户名
  101. * @param string $nickname 昵称
  102. * @param string $bio 个人简介
  103. */
  104. public function profile()
  105. {
  106. //验证
  107. if($this->auth->type != 1){
  108. $this->error('只有门店老板才能设置');
  109. }
  110. $field = [
  111. 'mobile',
  112. 'image',
  113. 'is_open',
  114. 'open_hours',
  115. ];
  116. $data = request_post_hub($field);
  117. $data['updatetime'] = time();
  118. $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
  119. $this->success('资料更新完成');
  120. }
  121. /**
  122. * 设置店铺地址
  123. */
  124. public function setaddress()
  125. {
  126. //验证
  127. if($this->auth->type != 1){
  128. $this->error('只有门店老板才能设置');
  129. }
  130. $field = [
  131. 'province_name',
  132. 'city_name',
  133. 'area_name',
  134. 'province_id',
  135. 'city_id',
  136. 'area_id',
  137. 'address',
  138. ];
  139. $data = request_post_hub($field);
  140. $data['full_address'] = $data['province_name'].$data['city_name'].$data['area_name'].$data['address'];
  141. $data['updatetime'] = time();
  142. $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
  143. $this->success('资料更新完成');
  144. }
  145. /**
  146. * 小程序码
  147. * @return void
  148. */
  149. public function getMiniCode()
  150. {
  151. try {
  152. $companyId = $this->auth->company_id;
  153. $companyWhere['id'] = $companyId;
  154. $companyWhere['status'] = 1;
  155. $company = Db::name('company')->where($companyWhere)->find();
  156. if (empty($company)) {
  157. throw new Exception('未找到门店信息');
  158. }
  159. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  160. if (empty($company['mini_code'])) {
  161. $client = new Client();
  162. $tk = getAccessToken();
  163. $miniCodeConfig = config('param.mini_code');
  164. $miniCodeConfig['scene'] = 'shopId='.$companyId;
  165. $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
  166. 'json' => $miniCodeConfig,
  167. ]);
  168. $fileName = md5($companyId);
  169. $fileUrl = '/uploads/company/'.$fileName.'.png';
  170. $code = $res2->getBody()->getContents();
  171. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  172. $companyData['mini_code'] = $fileUrl;
  173. $companyRes = Db::name('company')->where($companyWhere)->update($companyData);
  174. if (!$companyRes) {
  175. throw new Exception('更新门店信息失败');
  176. }
  177. $miniCode = $httpStr.$fileUrl;
  178. } else {
  179. $miniCode = $httpStr.$company['mini_code'];
  180. }
  181. $result = [
  182. 'mini_code' => $miniCode,
  183. 'company_name' => $this->auth->company->name,
  184. 'company_image' => one_domain_image($this->auth->company->image),
  185. ];
  186. $this->success('获取成功',$result);
  187. } catch (Exception $e) {
  188. $this->error($e->getMessage());
  189. }
  190. }
  191. /**
  192. * 获取用户openid
  193. */
  194. public function getUserOpenid() {
  195. // code值
  196. $code = $this->request->param('code');
  197. if (!$code) {
  198. $this->error(__('Invalid parameters'));
  199. }
  200. $config = config('company_wxMiniProgram');
  201. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  202. $openidInfo = $this->getJson($getopenid);
  203. if(!isset($openidInfo['openid'])) {
  204. $this->error('用户openid获取失败',$openidInfo);
  205. }
  206. // 获取的结果存入数据库
  207. /*$find = Db::name('company_sessionkey')->where(['openid'=>$openidInfo['openid']])->find();
  208. if($find) {
  209. $update = [];
  210. $update['sessionkey'] = $openidInfo['session_key'];
  211. $update['createtime'] = time();
  212. $res = Db::name('company_sessionkey')->where(['openid'=>$openidInfo['openid']])->update($update);
  213. } else {
  214. $insert = [];
  215. $insert['sessionkey'] = $openidInfo['session_key'];
  216. $insert['openid'] = $openidInfo['openid'];
  217. $insert['unionid'] = isset($openidInfo['unionid']) ? $openidInfo['unionid'] : '';
  218. $insert['createtime'] = time();
  219. $res = Db::name('company_sessionkey')->insertGetId($insert);
  220. }*/
  221. if(!empty($openidInfo)) {
  222. $this->success('获取成功',$openidInfo);
  223. } else {
  224. $this->error('获取失败');
  225. }
  226. }
  227. /**
  228. * json 请求
  229. * @param $url
  230. * @return mixed
  231. */
  232. private function getJson($url){
  233. $ch = curl_init();
  234. curl_setopt($ch, CURLOPT_URL, $url);
  235. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  236. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  237. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  238. $output = curl_exec($ch);
  239. curl_close($ch);
  240. return json_decode($output, true);
  241. }
  242. }