123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace app\common\model;
- use app\common\model\Order;
- use app\common\model\Sms;
- use Firebase\JWT\JWT;
- use think\Model;
- use app\common\library\Common;
- use think\facade\Validate;
- use think\Request;
- use think\Db;
- use app\common\library\WxService;
- use app\common\library\WxPay;
- use app\common\library\AliPay;
- use EasyWeChat\Factory;
- header('Access-Control-Allow-Origin: *');
- /**
- * 会员模型
- */
- class User Extends Model
- {
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'TIMESTAMP';
- // 定义时间戳字段名
- protected $createTime = 'create_at';
- protected $updateTime = 'update_at';
- // 追加属性
- protected $append = [
- ];
- protected $table = 'store_member';
- /**
- * 微信授权登录、注册
- */
- public static function wechatLogin($code,$rawData){
- $appid = 'wx36e94e688b2db929';
- $secret = '475d867175d57f50fc70845b9d67548d';
- $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
- $session_key = Common::curlRequest($url);
- if (!empty($session_key['session_key'])) {
- $openid = $session_key['openid'];
- //数据库是否已注册
- $user = Db::name('store_member')
- ->where('openid',$openid)
- ->find();
- if ($user){
- $token = JWT::encode($user,config('jwt.key'));
- $result['token'] = $token;
- $result['vip_level'] = $user['vip_level'];
- return Common::return_success('授权成功',$result);
- }else{
- if ($rawData){
- $data['rawdata'] = $rawData;
- $rawData = json_decode($rawData,true);
- $data['nickname'] = $rawData['nickName'];
- $data['openid'] = $openid;
- $data['sex'] = 1;
- $data['headimg'] = $rawData['avatarUrl'];
- $data['vip_number']=rand(100,999).rand(100,999).rand(1,9);
- $data['vip_level'] = 4;
- }
- Db::startTrans();
- try {
- $user = self::create($data);
- Db::commit();
- $userId =$user->id;
- $use = Db::name('store_member')->where('id',$userId)->find();
- $url = 'https://xiangqinim.zhousi.hdlkeji.com/addons/fastim/api.user/createUser?username='.$use['nickname'].'&avatar='.$use['headimg'].'&id='.$userId;
- $im_id = curlRequest($url);
- Db::name('store_member')->where('id',$userId)->update(['im_id'=>$im_id]);
- $token = JWT::encode($use,config('jwt.key'));
- $result['token'] = $token;
- $result['vip_level'] = $user['vip_level'];
- return Common::return_success('授权成功',$result);
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('授权失败');
- }
- }
- }else{
- return Common::return_error('获取session_key失败!');
- }
- }
- /**
- * 微信授权登录、注册
- */
- public static function PhoneLogin($phone){
- $user = Db::name('store_member')
- ->where('phone',$phone)
- ->find();
- if ($user){
- $token = JWT::encode($user,config('jwt.key'));
- $result['token'] = $token;
- $result['vip_level'] = $user['vip_level'];
- return Common::return_success('授权成功',$result);
- }else{
- $data['phone'] = $phone;
- $data['sex'] = 1;
- $data['headimg'] = common_url().'/main/start.png';
- $data['vip_number']=rand(100,999).rand(100,999).rand(1,9);
- $data['vip_level'] = 4;
- Db::startTrans();
- try {
- $user = self::create($data);
- Db::commit();
- $userId =$user->id;
- $use = Db::name('store_member')->where('id',$userId)->find();
- $url = 'https://xiangqinim.zhousi.hdlkeji.com/addons/fastim/api.user/createUser?username='.$use['nickname'].'&avatar='.$use['headimg'].'&id='.$userId;
- $im_id = curlRequest($url);
- Db::name('store_member')->where('id',$userId)->update(['im_id'=>$im_id]);
- $token = JWT::encode($use,config('jwt.key'));
- $result['token'] = $token;
- $result['vip_level'] = $user['vip_level'];
- return Common::return_success('授权成功',$result);
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('授权失败');
- }
- }
- }
- /**
- * 根据手机号获取用户信息
- */
- public static function getByMobile($phone){
- $userinfo = self::where('phone',$phone)->find();
- return $userinfo;
- }
- /**
- * 获取用户信息
- */
- public static function getUserInfo($id){
- $info = self::where('id',$id)->find();
- return $info;
- }
- /**
- * 修改密码
- */
- public static function editPayPassword($user_id,$ver_code,$password){
- $phone_use = self::where('id',$user_id)->find();
- //获取最后的验证码
- $time = time()-90;
- $sms = Sms::where(['mobile' => $phone_use['phone'], 'event' => 'changepwd'])
- ->where('createtime','>',$time)
- ->order('id', 'DESC')
- ->find();
- if (!$sms || $sms->code != $ver_code){
- return Common::return_error('短信验证码不正确!');
- }
- $phone_use->pay_password = md5($password);
- Db::startTrans();
- try {
- $phone_use->save();
- Db::commit();
- return Common::return_success('修改成功');
- }catch (Exception $e) {
- Db::rollback();
- return Common::return_error('修改失败');
- }
- }
- /**
- * 编辑用户资料
- */
- public static function EditUserInfo($user_id,$data){
- if (self::where('id',$user_id)->update($data)){
- return Common::return_success('编辑成功');
- }else{
- return Common::return_error('编辑失败');
- }
- }
- /**
- * 我的送检人列表
- */
- public static function peoplelist($user_id){
- $list = MakesPeople::where('user_id',$user_id)
- ->order('id asc')
- ->select();
- return Common::return_success('成功',$list);
- }
- /**
- * 添加,修改送检人
- */
- public static function AddEditPeoplelist($user_id,$data,$id){
- if ($id){
- $address = MakesPeople::where('id',$id)->where('user_id',$user_id)->find();
- if (!$address) return Common::return_error('信息不存在');
- //编辑
- if ($address->save($data)){
- return Common::return_success('编辑成功');
- }else{
- return Common::return_error('编辑失败');
- }
- }else{
- $data['user_id'] = $user_id;
- $addre = MakesPeople::create($data);
- if ($addre){
- return Common::return_success('添加成功');
- }else{
- return Common::return_error('添加失败');
- }
- }
- }
- /**
- * 删除送检人
- */
- public static function DelPeoplelist($user_id,$id){
- $address = MakesPeople::where('id',$id)->where('user_id',$user_id)->find();
- if (!$address) return Common::return_error('信息不存在');
- if (MakesPeople::where('id',$id)->delete()){
- return Common::return_success('删除成功');
- }else{
- return Common::return_error('删除失败');
- }
- }
- /**
- * 变更会员余额
- * @param int $money 余额
- * @param int $user_id 会员ID
- * @param string $memo 备注
- */
- public static function money($money, $user_id, $memo, $type,$pm = 0,$phone='',$fee=0,$status=0)
- {
- $user = self::get($user_id);
- if ($user)
- {
- switch ($type){
- case 1:
- $user_money = $user->equity_integral;
- $field = 'equity_integral';
- break;
- case 2:
- $user_money = $user->usable_integral;
- $field = 'usable_integral';
- break;
- case 3:
- $user_money = $user->repertory_integral;
- $field = 'repertory_integral';
- break;
- case 4:
- $user_money = $user->coupon;
- $field = 'coupon';
- break;
- }
- $before = $user_money;
- if ($pm==1){
- $after = $user_money + $money;
- }else{
- $after = $user_money - $money;
- }
- //更新会员信息
- $user->save([$field => $after]);
- //写入日志
- MoneyLog::create(['user_id' => $user_id,'pm' => $pm, 'change_money' => $money, 'before' => $before, 'after' => $after, 'title' => $memo,'type'=>$type,'phone'=>$phone,'fee'=>$fee,'status'=>$status]);
- }
- }
- }
|