You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
8.1 KiB

7 years ago
7 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Logic;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Common\ReturnData;
  6. use App\Common\Token;
  7. use App\Http\Model\User;
  8. use App\Http\Requests\UserRequest;
  9. use Validator;
  10. use App\Http\Model\UserMoney;
  11. class UserLogic extends BaseLogic
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. }
  17. public function getModel()
  18. {
  19. return new User();
  20. }
  21. public function getValidate($data, $scene_name)
  22. {
  23. //数据验证
  24. $validate = new UserRequest();
  25. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  26. }
  27. //列表
  28. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  29. {
  30. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  31. if($res['count'] > 0)
  32. {
  33. foreach($res['list'] as $k=>$v)
  34. {
  35. $res['list'][$k] = $this->getDataView($v);
  36. //$res['list'][$k]->user_name = !empty($res['list'][$k]->mobile) ? $res['list'][$k]->mobile : $res['list'][$k]->user_name;
  37. }
  38. }
  39. return $res;
  40. }
  41. //分页html
  42. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  43. {
  44. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  45. if($res->count() > 0)
  46. {
  47. foreach($res as $k=>$v)
  48. {
  49. $res[$k] = $this->getDataView($v);
  50. }
  51. }
  52. return $res;
  53. }
  54. //全部列表
  55. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  56. {
  57. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  58. if($res)
  59. {
  60. foreach($res as $k=>$v)
  61. {
  62. $res[$k] = $this->getDataView($v);
  63. }
  64. }
  65. return $res;
  66. }
  67. //详情
  68. public function getOne($where = array(), $field = '*')
  69. {
  70. $res = $this->getModel()->getOne($where, $field);
  71. if(!$res){return false;}
  72. $res = $this->getDataView($res);
  73. $res->reciever_address = model('UserAddress')->getOne(['id'=>$res->address_id]);
  74. $res->collect_goods_count = model('CollectGoods')->getDb()->where(['user_id'=>$res->id])->count();
  75. $res->bonus_count = model('UserBonus')->getDb()->where(array('user_id'=>$res->id,'status'=>0))->count();
  76. return $res;
  77. }
  78. //添加
  79. public function add($data = array(), $type=0)
  80. {
  81. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  82. $validator = $this->getValidate($data, 'add');
  83. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  84. $data['add_time'] = $data['updated_at'] = time();//添加、更新时间
  85. $res = $this->getModel()->add($data,$type);
  86. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  87. return ReturnData::create(ReturnData::FAIL);
  88. }
  89. //修改
  90. public function edit($data, $where = array())
  91. {
  92. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  93. $validator = $this->getValidate($data, 'edit');
  94. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  95. $data['updated_at'] =time();
  96. $res = $this->getModel()->edit($data,$where);
  97. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  98. return ReturnData::create(ReturnData::FAIL);
  99. }
  100. //删除
  101. public function del($where)
  102. {
  103. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  104. $validator = $this->getValidate($where,'del');
  105. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  106. $res = $this->getModel()->del($where);
  107. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  108. return ReturnData::create(ReturnData::FAIL);
  109. }
  110. /**
  111. * 数据获取器
  112. * @param array $data 要转化的数据
  113. * @return array
  114. */
  115. private function getDataView($data = array())
  116. {
  117. return getDataAttr($this->getModel(),$data);
  118. }
  119. //修改用户密码、支付密码
  120. public function userPasswordUpdate($data, $where = array())
  121. {
  122. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  123. $user = $this->getModel()->getOne($where);
  124. if(!$user){return ReturnData::create(ReturnData::PARAMS_ERROR, null, '用户不存在');}
  125. //密码
  126. if(isset($data['old_password']))
  127. {
  128. if($user->password && $data['old_password']!=$user->password)
  129. {
  130. return ReturnData::create(ReturnData::PARAMS_ERROR, null, '旧密码错误');
  131. }
  132. }
  133. //支付密码
  134. if(isset($data['pay_password']))
  135. {
  136. if($user->pay_password && $data['old_pay_password']!=$user->pay_password)
  137. {
  138. return ReturnData::create(ReturnData::PARAMS_ERROR, null, '旧支付密码错误');
  139. }
  140. }
  141. $res = $this->getModel()->edit($data,$where);
  142. if($res === false){return ReturnData::create(ReturnData::SYSTEM_FAIL);}
  143. return ReturnData::create(ReturnData::SUCCESS,$res);
  144. }
  145. //签到
  146. public function signin($where)
  147. {
  148. $user = $this->getModel()->getOne($where);
  149. if(!$user){return ReturnData::create(ReturnData::PARAMS_ERROR, null, '用户不存在');}
  150. $signin_time='';
  151. if(!empty($user->signin_time)){$signin_time = date('Ymd',strtotime($user->signin_time));} //签到时间
  152. $today = date('Ymd',time()); //今日日期
  153. if($signin_time==$today){return ReturnData::create(ReturnData::PARAMS_ERROR, null, '今日已签到');}
  154. $signin_point = (int)sysconfig('CMS_SIGN_POINT'); //签到积分
  155. $this->getModel()->edit(['point'=>($user->point+$signin_point),'signin_time'=>date('Y-m-d H:i:s')], ['id'=>$user->id]); //更新用户积分,及签到时间
  156. model('UserPoint')->add(['type'=>1,'point'=>$signin_point,'des'=>'签到','user_id'=>$user->id]); //添加签到积分记录
  157. return ReturnData::create(ReturnData::SUCCESS, null, '签到成功');
  158. }
  159. //用户登录
  160. public function wxLogin($where)
  161. {
  162. if(isset($where['openid']) && !empty($where['openid']))
  163. {
  164. $user = $this->getOne(array('openid'=>$where['openid']));
  165. }
  166. elseif(isset($where['user_name']) && !empty($where['user_name']))
  167. {
  168. $user = $this->getOne(function ($query) use ($where) {$query->where(['mobile'=>$where['user_name'],'password'=>$where['password']])->orWhere(['user_name'=>$where['user_name'],'password'=>$where['password']]);});
  169. }
  170. if(isset($user) && !$user){return ReturnData::create(ReturnData::PARAMS_ERROR, null, '用户不存在或者账号密码错误');}
  171. $token = Token::getToken(Token::TYPE_WEIXIN, $user->id);
  172. foreach($token as $k=>$v)
  173. {
  174. $user->$k = $v;
  175. }
  176. return ReturnData::create(ReturnData::SUCCESS, $user, '登录成功');
  177. }
  178. //注册
  179. public function wxRegister($data)
  180. {
  181. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  182. $data['add_time'] = time();
  183. $validator = $this->getValidate($data, 'wx_register');
  184. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  185. $res = $this->getModel()->add($data);
  186. if($res === false){return ReturnData::create(ReturnData::SYSTEM_FAIL);}
  187. //生成token
  188. $token = Token::getToken(Token::TYPE_WEIXIN, $res);
  189. return ReturnData::create(ReturnData::SUCCESS,$token, '注册成功');
  190. }
  191. }