123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- namespace app\api\controller\company;
- use app\common\controller\Apic;
- use app\common\library\Sms;
- use fast\Random;
- use GuzzleHttp\Client;
- use think\Config;
- use think\Exception;
- use think\Validate;
- use think\Db;
- class User extends Apic
- {
- protected $noNeedLogin = ['accountlogin','resetpwd','getUserOpenid'];
- protected $noNeedRight = '*';
-
- public function accountlogin(){
- $mobile = $this->request->post('mobile');
- $password = $this->request->post('password');
- $openid = $this->request->post('openid','');
- if (!$mobile || !$password || !$openid) {
- $this->error(__('Invalid parameters'));
- }
- $ret = $this->auth->login($mobile, $password, $openid);
- if ($ret) {
- $data = $this->auth->getUserinfo();
- $this->success(__('Logged in successful'), $data);
- } else {
- $this->error($this->auth->getError());
- }
- }
-
- public function logout()
- {
- if (!$this->request->isPost()) {
- $this->error(__('Invalid parameters'));
- }
- $this->auth->logout();
- $this->success(__('Logout successful'));
- }
-
- public function getUserinfo($type = 1){
- $info = $this->auth->getUserinfo();
- if($type == 'return'){
- return $info;
- }
- $this->success(__('success'),$info);
- }
-
- public function resetpwd()
- {
- $mobile = $this->request->post('mobile');
- $captcha = $this->request->post('captcha');
- $newpassword = $this->request->post("newpassword");
- if (!$mobile || !$captcha || !$newpassword) {
- $this->error(__('Invalid parameters'));
- }
-
- if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
- $this->error(__('Password must be 6 to 30 characters'));
- }
- if (!Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('Mobile is incorrect'));
- }
- $user = \app\common\model\CompanyStaff::getByMobile($mobile);
- if (!$user) {
- $this->error(__('User not found'));
- }
- $ret = Sms::check($mobile, $captcha, 'resetpwd');
- if (!$ret) {
- $this->error(__('Captcha is incorrect'));
- }
- Sms::flush($mobile, 'resetpwd');
-
- $this->auth->direct($user->id);
- $ret = $this->auth->resetpwd($newpassword, '', true);
- if ($ret) {
- $this->success(__('Reset password successful'));
- } else {
- $this->error($this->auth->getError());
- }
- }
-
- public function profile()
- {
-
- if($this->auth->type != 1){
- $this->error('只有门店老板才能设置');
- }
- $field = [
- 'mobile',
- 'image',
- 'is_open',
- 'open_hours',
- ];
- $data = request_post_hub($field);
- $data['updatetime'] = time();
- $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
- $this->success('资料更新完成');
- }
-
- public function setaddress()
- {
-
- if($this->auth->type != 1){
- $this->error('只有门店老板才能设置');
- }
- $field = [
- 'province_name',
- 'city_name',
- 'area_name',
- 'province_id',
- 'city_id',
- 'area_id',
- 'address',
- ];
- $data = request_post_hub($field);
- $data['full_address'] = $data['province_name'].$data['city_name'].$data['area_name'].$data['address'];
- $data['updatetime'] = time();
- $update_rs = Db::name('company')->where('id',$this->auth->company_id)->update($data);
- $this->success('资料更新完成');
- }
-
- public function getMiniCode()
- {
- try {
- $companyId = $this->auth->company_id;
- $companyWhere['id'] = $companyId;
- $companyWhere['status'] = 1;
- $company = Db::name('company')->where($companyWhere)->find();
- if (empty($company)) {
- throw new Exception('未找到门店信息');
- }
- $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
- if (empty($company['mini_code'])) {
- $client = new Client();
- $tk = getAccessToken();
- $miniCodeConfig = config('param.mini_code');
- $miniCodeConfig['scene'] = 'shopId='.$companyId;
- $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
- 'json' => $miniCodeConfig,
- ]);
- $fileName = md5($companyId);
- $fileUrl = '/uploads/company/'.$fileName.'.png';
- $code = $res2->getBody()->getContents();
- file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
- $companyData['mini_code'] = $fileUrl;
- $companyRes = Db::name('company')->where($companyWhere)->update($companyData);
- if (!$companyRes) {
- throw new Exception('更新门店信息失败');
- }
- $miniCode = $httpStr.$fileUrl;
- } else {
- $miniCode = $httpStr.$company['mini_code'];
- }
- $result = [
- 'mini_code' => $miniCode,
- 'company_name' => $this->auth->company->name,
- 'company_image' => one_domain_image($this->auth->company->image),
- ];
- $this->success('获取成功',$result);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
-
- public function getUserOpenid() {
-
- $code = $this->request->param('code');
- if (!$code) {
- $this->error(__('Invalid parameters'));
- }
- $config = config('company_wxMiniProgram');
- $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
- $openidInfo = $this->getJson($getopenid);
- if(!isset($openidInfo['openid'])) {
- $this->error('用户openid获取失败',$openidInfo);
- }
-
-
- if(!empty($openidInfo)) {
- $this->success('获取成功',$openidInfo);
- } else {
- $this->error('获取失败');
- }
- }
-
- private function getJson($url){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($ch);
- curl_close($ch);
- return json_decode($output, true);
- }
- }
|