Companys.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. class Companys extends Api
  7. {
  8. protected $noNeedLogin = ['getList','getInfo'];
  9. protected $noNeedRight = '*';
  10. protected $model = null;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = Db::name('company');
  15. }
  16. /**
  17. * 列表
  18. * @return void
  19. */
  20. public function getList()
  21. {
  22. try {
  23. $page = $this->request->param('page',1);
  24. $limit = $this->request->param('listrow',15);
  25. $offset = ($page - 1) * $limit;
  26. //获取门店信息
  27. $params = [
  28. 'lng' => $this->request->param('lng',''),
  29. 'lat' => $this->request->param('lat',''),
  30. ];
  31. $companyList = $this->getCompanySort($params);
  32. $result = array_slice($companyList['data'],$offset,$limit);
  33. $this->success('获取成功',$result);
  34. } catch (Exception $e) {
  35. $this->error($e->getMessage());
  36. }
  37. }
  38. /**
  39. * 获取门店距离排序
  40. * @return void
  41. */
  42. public function getCompanySort($params=[])
  43. {
  44. try {
  45. $result = [
  46. 'status' => true,
  47. 'msg' => '获取成功',
  48. 'data' => [],
  49. ];
  50. $lng = isset($params['lng']) ? $params['lng'] : '';
  51. $lat = isset($params['lat']) ? $params['lat'] : '';
  52. $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
  53. $where['status'] = 1;
  54. $data = $this->model->where($where)->field($field)->select();
  55. if (!empty($data)) {
  56. foreach ($data as $key => &$value) {
  57. $value['image'] = !empty($value['image']) ? cdnurl($value['image']) : '';
  58. $value['distance'] = getDistance($lng, $lat, $value['longitude'], $value['latitude'], true, 2);
  59. $value['distance_text'] = !empty($value['distance']) ? $value['distance'].'km' : '';
  60. }
  61. $n = count($data);
  62. for($i=1;$i<$n;$i++) { //从第二个元素开始插入
  63. for($j=$i-1;$j>=0;$j--) { //与前面的数比较,找到插入的位置
  64. if($data[$j]['distance'] > $data[$j+1]['distance']) { //比前面的数小,交换位置
  65. $tmp = $data[$j];
  66. $data[$j] = $data[$j+1];
  67. $data[$j+1] = $tmp;
  68. } else { //大于或等于前面的数,表示已找到插入的位置
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. $result['data'] = $data;
  75. return $result;
  76. } catch (Exception $e) {
  77. $result['status'] = 0;
  78. $result['msg'] = $e->getMessage();
  79. return $result;
  80. }
  81. }
  82. /**
  83. * 详情
  84. * @return void
  85. */
  86. public function getInfo()
  87. {
  88. try {
  89. $id = $this->request->param('id',0);
  90. $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
  91. $where['status'] = 1;
  92. $where['id'] = $id;
  93. $result = $this->model->where($where)->field($field)->find();
  94. if (!empty($result)) {
  95. !empty($result['image']) && $result['image'] = cdnurl($result['image']);
  96. }
  97. $this->success('获取成功',$result);
  98. } catch (Exception $e) {
  99. $this->error($e->getMessage());
  100. }
  101. }
  102. /**
  103. * 绑定门店
  104. * @return void
  105. */
  106. public function bind()
  107. {
  108. Db::startTrans();
  109. try {
  110. $id = $this->request->param('id',0);
  111. $comeform = $this->request->param('comeform','');//'自然进店','平台引流','线下新客','老带新'
  112. $where['id'] = $id;
  113. $where['status'] = 1;
  114. $company = $this->model->where($where)->find();
  115. if (empty($company)) {
  116. throw new Exception('未找到门店信息');
  117. }
  118. $user = $this->auth->getUser();
  119. $userId = $this->auth->id;
  120. if ($user['company_id'] != $id) {//更新绑定门店
  121. $userWhere['id'] = $userId;
  122. $userData['company_id'] = $id;
  123. $userRes = model('User')->where($userWhere)->update($userData);
  124. if (!$userRes) {
  125. throw new Exception('绑定门店失败');
  126. }
  127. }
  128. //用户门店钱包
  129. $walletWhere['user_id'] = $userId;
  130. $walletWhere['company_id'] = $id;
  131. $userWallet = model('UserWallet')->where($walletWhere)->find();
  132. if (empty($userWallet)) {
  133. $walletData = [
  134. 'user_id' => $userId,
  135. 'company_id' => $id,
  136. 'money' => 0.00,
  137. 'comefrom' => $comeform,
  138. ];
  139. $walletRes = model('UserWallet')->insertGetId($walletData);
  140. if (!$walletRes) {
  141. throw new Exception('绑定失败');
  142. }
  143. }
  144. Db::commit();
  145. $this->success();
  146. } catch (Exception $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. }
  150. }
  151. /**
  152. * 保存
  153. * @return void
  154. */
  155. public function save()
  156. {
  157. try {
  158. //验证参数
  159. $id = $this->request->param('id',0);
  160. $userId = $this->auth->id;
  161. if (empty($id)) {
  162. $companyWhere['user_id'] = $userId;
  163. $company = $this->model->where($companyWhere)->find();
  164. if (!empty($company)) {
  165. $id = $company['id'];
  166. }
  167. }
  168. $scene = !empty($id) ? 'edit' : 'add';
  169. $validate = validate('Companys');
  170. if(!$validate->check($this->request->param(),[],$scene)){
  171. throw new Exception($validate->getError());
  172. }
  173. if (!empty($id)) {
  174. $where['user_id'] = $userId;
  175. $where['id'] = $id;
  176. $companyData = $this->model->where($where)->find();
  177. if (empty($companyData)) {
  178. throw new Exception('未找到相关信息');
  179. }
  180. } else {
  181. $where['user_id'] = $userId;
  182. $where['status'] = 1;//状态:-1=新来的,0=待审核,1=审核通过,2=审核不通过
  183. $companyData = $this->model->where($where)->find();
  184. if (!empty($companyData)) {
  185. throw new Exception('您已申请过入驻');
  186. }
  187. }
  188. $time = time();
  189. $areaData = getProvince($this->request->param());
  190. $fullAddress = $areaData['full_address'];
  191. $data = [
  192. 'contacts' => $this->request->param('contacts', ''),
  193. 'mobile' => $this->request->param('mobile', ''),
  194. 'province_id' => $this->request->param('province_id', 0),
  195. 'city_id' => $this->request->param('city_id', 0),
  196. 'area_id' => $this->request->param('area_id', 0),
  197. 'province_name' => $areaData['province_name'],
  198. 'city_name' => $areaData['city_name'],
  199. 'area_name' => $areaData['area_name'],
  200. 'address' => $this->request->param('address', ''),
  201. 'full_address' => $fullAddress,
  202. 'aptitude_images' => $this->request->param('aptitude_images', ''),
  203. 'status' => 0,
  204. ];
  205. if (empty($id)) {
  206. $data['image'] = '/assets/img/avatar.png';//默认头像
  207. $data['user_id'] = $userId;
  208. $data['createtime'] = $time;
  209. $res = $this->model->insertGetId($data);
  210. } else {
  211. $data['updatetime'] = $time;
  212. $where['id'] = $id;
  213. $where['user_id'] = $userId;
  214. $res = $this->model->where($where)->update($data);
  215. }
  216. if (!$res) {
  217. throw new Exception('操作失败');
  218. }
  219. $this->success('操作成功');
  220. } catch (Exception $e) {
  221. $this->error($e->getMessage());
  222. }
  223. }
  224. /**
  225. * 入驻详情
  226. * @return void
  227. */
  228. public function getCompanyInfo()
  229. {
  230. try {
  231. $userId = $this->auth->id;
  232. $field = 'id,contacts,mobile,province_id,city_id,area_id,province_name,city_name,area_name,address,aptitude_images,status';
  233. $where['user_id'] = $userId;
  234. $result = $this->model->where($where)->field($field)->find();
  235. if (!empty($result)) {
  236. $statusArr = model('Company')->getStatusList();
  237. $result['status_text'] = isset($statusArr[$result['status']]) ? $statusArr[$result['status']] : '';
  238. $result = info_domain_image($result,['aptitude_images']);
  239. }
  240. $this->success('获取成功',$result);
  241. } catch (Exception $e) {
  242. $this->error($e->getMessage());
  243. }
  244. }
  245. }