AlbumController.php 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\AgentBannerModel;
  4. use App\Models\AlbumAgentModel;
  5. use App\Models\AlbumBannerModel;
  6. use App\Models\AlbumCatModel;
  7. use App\Models\AlbumCommentsModel;
  8. use App\Models\AlbumFavoriteModel;
  9. use App\Models\AlbumFormId;
  10. use App\Models\AlbumInformationModel;
  11. use App\Models\AlbumManufacturerModel;
  12. use App\Models\AlbumNavModel;
  13. use App\Models\AlbumNewsModel;
  14. use App\Models\AlbumProductModel;
  15. use App\Models\AlbumProductPriceModel;
  16. use App\Models\AlbumProductStyleModel;
  17. use App\Models\AlbumUserModel;
  18. use App\Models\AlbumWatchRecord;
  19. use App\Models\AlbumXyxUserModel;
  20. use App\Models\CustomerCatRecordModel;
  21. use App\Models\CustomerDetailsModel;
  22. use App\User;
  23. use Illuminate\Http\Request;
  24. use Illuminate\Support\Facades\DB;
  25. use Validator, Response,Auth;
  26. use App\Services\Base\ErrorCode;
  27. use WXBizDataCrypt;
  28. use EasyWeChat\Factory;
  29. class AlbumController extends Controller
  30. {
  31. private $wechat_app;
  32. public function albumSetting(Request $request)
  33. {
  34. $validator = Validator::make($request->all(), [
  35. 'store_id' => 'required',
  36. ], [
  37. 'store_id.required' => '店铺信息未知',
  38. ]);
  39. if ($validator->fails()) {
  40. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
  41. }
  42. $store_id = $request->store_id;
  43. //小程序設置
  44. $setting = AlbumManufacturerModel::where('store_id',$store_id)->first();
  45. //轮播图
  46. $banner = AlbumBannerModel::where('store_id',$store_id)->orderByDesc('sort')->get();
  47. //底部导航栏
  48. $navbar = AlbumNavModel::where([['store_id',$store_id],['state',0]])->orderByDesc('sort')->get();
  49. //顶部菜单栏
  50. $menus = AlbumNavModel::where([['store_id',$store_id],['state',1]])->orderByDesc('sort')->get();
  51. $news = AlbumNewsModel::where([['store_id',$store_id]])->orderByDesc('sort')->paginate(5);
  52. return $this->api(compact('setting','banner','news','navbar','menus'));
  53. }
  54. /**
  55. * @api {post} /api/album/xyx_login 登陆(xyx_login)
  56. * @apiDescription 登陆(xyx_login)
  57. * @apiGroup Album
  58. * @apiPermission none
  59. * @apiVersion 0.1.0
  60. * @apiParam {int} [code]
  61. * @apiParam {int} [agent_id] 经销商id
  62. * @apiParam {int} [store_id] 商户id
  63. * @apiSuccessExample {json} Success-Response:
  64. * HTTP/1.1 200 OK
  65. * {
  66. * "status": true,
  67. * "status_code": 0,
  68. * "message": "",
  69. * "data": {
  70. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImIyNGI3MzY3MDNmZDg3MGFjNTg2MWIxNDUzMDcyYjljYzFmNmJhMzE2NTAxZWVlNGYzM2M5MTIzNzFmNGYzZjg2MTY1M2YxMjE2YzE4OTFiIn0.eyJhdWQiOiI5IiwianRpIjoiYjI0YjczNjcwM2ZkODcwYWM1ODYxYjE0NTMwNzJiOWNjMWY2YmEzMTY1MDFlZWU0ZjMzYzkxMjM3MWY0ZjNmODYxNjUzZjEyMTZjMTg5MWIiLCJpYXQiOjE1NDA3OTYyMTYsIm5iZiI6MTU0MDc5NjIxNiwiZXhwIjoxNTcyMzMyMjE1LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.Ruihvl_HMCAHx9XDfeckz48Q_72kfG7vWDsqDrZWbrjq-yuNpM3LrUF-nlsKjedo97BwU_Apz-jnGVqr0ONKZlH-aBKDmIgM2aUZfFADsHhHNJH_oU2fMe1bOrjfUp-PJxfVWQS8c8h5RYmeqtzAYCKaD4P7OZbLmOX0YosKghpaGtiyE65s6jtrOXRhAzXAhYsZToQNBMokFVRqGih9EWuVp6EVwRtI3cb4IV6iQf08cX9DGlLLBJJzLWR5ZWgxJLAGrj5iMCWBl4JKVR8Fsy-xnx3EtrB7ODpJH6hB_u3XnUSAFuaG-KzMPLsj45iqg2hy53er2xtcfAowQFsPoAbZZu0n8c9hWt4GLemR6N1iZ3LBViv3tL5BK52pinQzAnmvltAhJP1YhfTOOjzkxcYVh2JeJagufEoxeeUarMMn21gQSFEzKFCimI7zZQxhT9leLpUn51DLD8Mu87NpYb3JN-JFbOlIDA7bDN3bNto7uuSuC4eBwPo3Ge0StuW4kjHsDTcrVF2He_8FdrPcPpGcvQsXQgv4vwlEhbL8dTlrOodum7H_tY_qUxUG1pEg6bfpn_82ej-AOSc9xi0nrhdcS9y_Z68rcwOJu8esI1pXm9TeQdZIDG85y_t74At8XN6oFcIM__6_dTne_3DHwijW-uOLupNky-5J4ILmZck",
  71. * "user": {
  72. * "user_id": 1,
  73. * "is_agent": 1,
  74. * "agent_id": 4
  75. * }
  76. * }
  77. * }
  78. * @apiErrorExample {json} Error-Response:
  79. * HTTP/1.1 400 Bad Request
  80. * {
  81. * "state": false,
  82. * "code": 1000,
  83. * "message": "传入参数不正确",
  84. * "data": null or []
  85. * }
  86. * 可能出现的错误代码:
  87. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  88. */
  89. public function albumXyxLogin(Request $request)
  90. {
  91. $datas = $request->input();
  92. $validator = Validator::make($request->all(),
  93. [
  94. 'code' => 'required',
  95. 'agent_id' => 'required',
  96. 'nickName' => 'required',
  97. 'iv' => 'required',
  98. 'encryptData' => 'required',
  99. 'avatar' => 'required',
  100. 'store_id' => 'required'
  101. ],[
  102. 'code.required' => 'code不能为空!',
  103. 'agent_id.required' => 'agent_id不能为空!',
  104. 'nickName.required' => '昵称不能为空!',
  105. 'iv.required' => '偏移量不能为空!',
  106. 'encryptData.required' => '加密数据不能为空!',
  107. 'avatar.required' => '头像不能为空!',
  108. 'store_id.required' => '站点ID不能为空!'
  109. ]
  110. );
  111. if ($validator->fails()) {
  112. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  113. }
  114. $this->wechat_app = AlbumManufacturerModel::where('store_id',$datas['store_id'])->first();
  115. if(!$this->wechat_app) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  116. $config = [
  117. 'app_id' => $this->wechat_app->xyx_id,
  118. 'secret' => $this->wechat_app->xyx_secret,
  119. // 下面为可选项
  120. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  121. 'response_type' => 'array',
  122. ];
  123. $app = Factory::miniProgram($config);
  124. $res = $app->auth->session($datas['code']);
  125. // dd($res);
  126. if (!$res || empty($res['openid'])) {
  127. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '获取用户OpenId失败!', $validator->messages());
  128. }
  129. $session = $res['session_key'];
  130. $decryptedData = $app->encryptor->decryptData($session, $datas['iv'], $datas['encryptData']);
  131. $check = AlbumUserModel::where('wechat_union_id',$decryptedData['unionId'])->first();
  132. if(!$check){
  133. $add['username'] = preg_replace('/[\xf0-\xf7].{3}/', '', $datas['nickName']);
  134. $add['open_id'] = $res['openid'];
  135. $add['wechat_union_id'] = $decryptedData['unionId'];
  136. $add['avatar'] = $datas['avatar'];
  137. $add['is_agent'] = 0;
  138. $add['model'] = 1;
  139. $add['store_id'] = $datas['store_id'];
  140. $res = AlbumUserModel::create($add);
  141. // dd($res);
  142. if($res){
  143. $check = AlbumUserModel::where([['wechat_union_id',$res->wechat_union_id],['store_id',$datas['store_id']]])->first();
  144. $user = [
  145. 'user_id'=>$check['id'],
  146. //'avatar'=>$check['avatar'],
  147. // 'username'=>$check['username'],
  148. 'phone'=>$check->phone,
  149. 'is_agent'=>0,
  150. 'agent_id'=>'',
  151. ];
  152. }else{
  153. $user=[
  154. 'error'=>1
  155. ];
  156. }
  157. } else {
  158. //print_r($check);die;
  159. $save['username'] = preg_replace('/[\xf0-\xf7].{3}/', '', $datas['nickName']);
  160. $save['avatar'] = $datas['avatar'];
  161. $save['open_id'] = $res['openid'];
  162. $save['model'] = 1;
  163. $res = AlbumUserModel::where('wechat_union_id',$decryptedData['unionId'])->update($save);
  164. //echo 111;
  165. //print_r($check);die;
  166. if($check->is_dealer == 1){
  167. $user_agent = AlbumAgentModel::where([['user_id',$check->id],['status',1]])->first();
  168. $agent_id = $user_agent['id'];
  169. } else {
  170. $agent_id = '';
  171. }
  172. $user = [
  173. 'user_id'=>$check->id,
  174. 'phone'=>$check->phone,
  175. //'avatar'=>$datas['avatar'],
  176. //'username'=>$datas['nickName'],
  177. 'is_agent'=>$check->is_dealer,
  178. 'agent_id'=>$agent_id,
  179. ];
  180. }
  181. // echo 111;die;
  182. if (Auth::loginUsingId($check->id)) {
  183. $userAuth = Auth::user();
  184. if($check->is_dealer ==0){
  185. if($request->input('agent_id')!=0){
  186. $userAuth->up_agent_id = $request->input('agent_id');
  187. }
  188. $add_record['agent_id'] = $request->input('agent_id');
  189. $add_record['open_id'] = $check->open_id;
  190. $add_record['action'] = 4;
  191. $add_record['store_id'] = $datas['store_id'];
  192. $add_record['detail'] = null;
  193. AlbumWatchRecord::create($add_record);
  194. // dd($add_record);
  195. // print_r($add_record['agent_id']);die;
  196. $user_agent = AlbumAgentModel::where('id',$add_record['agent_id'])->first();
  197. //dd($user_agent);
  198. if($user_agent){
  199. $user_agent->get_count++;
  200. $user_agent->save();
  201. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  202. try{
  203. $this->sendLogsMessage($datas['store_id'],$agent->open_id,4,$userAuth->username);
  204. }catch (\Exception $e){
  205. }
  206. }else{
  207. $userAuth->up_agent_id = 0;
  208. }
  209. }else{
  210. $userAuth->up_agent_id = 0;
  211. }//dd($userAuth);
  212. $userAuth->save();
  213. $user['up_agent_id'] = $userAuth->up_agent_id;
  214. $token = $userAuth->createToken($userAuth->id . '-' . $userAuth->openid)->accessToken;
  215. $conf = AlbumManufacturerModel::where('store_id',$datas['store_id'])->first();
  216. $getPhone = $conf->getPhone;
  217. return $this->api(compact('token', 'user','getPhone'));
  218. } else {
  219. return $this->error(ErrorCode::INCORRECT_USER_OR_PASS);
  220. }
  221. }
  222. /*
  223. * curl get请求
  224. */
  225. public function curl_get($url)
  226. {
  227. //初始化
  228. $ch = curl_init();
  229. //设置选项,包括URL
  230. curl_setopt($ch, CURLOPT_URL, $url);
  231. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  232. curl_setopt($ch, CURLOPT_HEADER, 0);
  233. $output = curl_exec($ch);
  234. curl_close($ch);
  235. return $output;
  236. }
  237. private function getOpenid($code)
  238. {
  239. $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_app->app_id}&secret={$this->wechat_app->app_secret}&js_code={$code}&grant_type=authorization_code";
  240. $res = $this->curl_get($api);
  241. $res = json_decode($res, true);
  242. return $res;
  243. }
  244. private function getOpenidxyx($code)
  245. {
  246. //$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_app->xyx_id}&secret={$this->wechat_app->xyx_secret}&js_code={$code}&grant_type=authorization_code";
  247. $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_app->xyx_id}&secret={$this->wechat_app->xyx_secret}&js_code={$code}&grant_type=authorization_code";
  248. $res = $this->curl_get($api);
  249. $res = json_decode($res, true);
  250. return $res;
  251. }
  252. /**
  253. * @api {get} /api/album/goods 商品列表(goods)
  254. * @apiDescription 商品列表(goods)
  255. * @apiGroup Album
  256. * @apiPermission none
  257. * @apiVersion 0.1.0
  258. * @apiParam {int} [cat_id] 分类id
  259. * @apiParam {string} [keywords] 关键词.
  260. * @apiParam {int} [status] 状态 1 只选择热销 2 只选择最新上市 3只选择一个风格 4 同时选择热销和最新上市 5同时选择热销和风格 6同时选择最新上市和风格 7全选 0全不选
  261. * @apiParam {int} [style] 风格
  262. * @apiSuccessExample {json} Success-Response:
  263. * HTTP/1.1 200 OK
  264. * {
  265. * "status": true,
  266. * "status_code": 0,
  267. * "message": "",
  268. * "data": {
  269. * "goods": [
  270. * {
  271. * "id": 3,
  272. * "cover_pic": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  273. * "name": "简约",
  274. * "style": "纯白",
  275. * "hot_cake": 1,
  276. * "news": 1,
  277. * "price": ""
  278. * }
  279. * ],
  280. * "name": 品牌名
  281. * }
  282. * }
  283. * @apiErrorExample {json} Error-Response:
  284. * HTTP/1.1 400 Bad Request
  285. * {
  286. * "state": false,
  287. * "code": 1000,
  288. * "message": "传入参数不正确",
  289. * "data": null or []
  290. * }
  291. * 可能出现的错误代码:
  292. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  293. */
  294. public function albumGoods(Request $request)
  295. {
  296. $userAuth = Auth('api')->user();
  297. if(!$userAuth) return $this->error(ErrorCode::ERROR_POWER, '登陆过期!');
  298. $validator = Validator::make($request->all(), [
  299. 'cat_id' => 'required',
  300. 'store_id' => 'required',
  301. 'status' => 'required',
  302. ],[
  303. 'cat_id.required'=>'缺少分类参数',
  304. 'store_id.required'=>'缺少商户参数',
  305. 'status.required'=>'缺少状态参数',
  306. ]);
  307. if ($validator->fails()) {
  308. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  309. }
  310. $data = $request->input();
  311. //dd($data);
  312. $cat_id = request('cat_id');
  313. $store_id = request('store_id');
  314. $status = request('status');
  315. $style = request('style');
  316. $select_info = ['cat_id'=>$cat_id,'style'=>$style];
  317. if($userAuth->up_agent_id!=0&&$cat_id!="undefined"&&$style!="undefined"){
  318. $add_record['agent_id'] = $userAuth->up_agent_id;
  319. $add_record['open_id'] = $userAuth->open_id;
  320. $add_record['action'] = 2;
  321. $add_record['store_id'] = $store_id;
  322. $add_record['detail'] = json_encode($select_info);
  323. AlbumWatchRecord::create($add_record);
  324. $cat_record['agent_id'] = $userAuth->up_agent_id;
  325. $cat_record['open_id'] = $userAuth->open_id;
  326. $cat_record['store_id'] = $store_id;
  327. $cat_record['style_id'] = $style;
  328. $cat_record['cat_id'] = $cat_id;
  329. CustomerCatRecordModel::create($cat_record);
  330. $user_agent = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  331. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  332. try{
  333. $this->sendLogsMessage($store_id,$agent->open_id,2,$userAuth->username);
  334. }catch (\Exception $e){
  335. }
  336. }
  337. if(isset($data['keywords'])&&!empty($data['keywords'])) {
  338. $keywords = '%'.$data['keywords'].'%';
  339. $goods = AlbumProductModel::where([['name','like',$keywords],['store_id',$data['store_id']],['cat_id',$cat_id],['store_id',$store_id]]);
  340. } else {
  341. $goods = AlbumProductModel::where([['cat_id',$cat_id],['store_id',$store_id]]);
  342. }
  343. if($status == 1) {
  344. $goods->where([['hot_cake', 1]]);
  345. }
  346. if($status == 2){
  347. $goods->where('news',1);
  348. }
  349. if($status == 3){
  350. $goods->where('style',$style);
  351. }
  352. $goods = $goods->select('id','cover_pic','name','style','hot_cake','news','price_default')->orderByDesc('sort')->get();
  353. foreach($goods as $key =>$val){
  354. if($userAuth->is_dealer != 1){
  355. if($userAuth->up_agent_id==0){
  356. $goods[$key]['price'] = '';
  357. }else{
  358. $check = AlbumProductPriceModel::where([['agent_id',$userAuth->up_agent_id],['store_id',$data['store_id']],['product_id',$val['id']]])->first();
  359. if($check){
  360. $goods[$key]['price'] = $check->price;
  361. }else{
  362. $goods[$key]['price'] = '';
  363. }
  364. }
  365. }elseif($userAuth->is_dealer == 1){
  366. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  367. $agent_id = $user_agent['id'];
  368. $check = AlbumProductPriceModel::where([['agent_id',$agent_id],['store_id',$data['store_id']],['product_id',$val['id']]])->first();
  369. if($check){
  370. $goods[$key]['price'] = $check->price;
  371. }else{
  372. $goods[$key]['price'] = '';
  373. }
  374. }
  375. }
  376. $data =AlbumManufacturerModel::where('store_id',$store_id)->first();
  377. $name = $data['name'];
  378. return $this->api(compact('goods','name'));
  379. }
  380. /**
  381. * @api {get} /api/album/goods-detail 商品详细(goods-detail)
  382. * @apiDescription 商品列表(goods-detail)
  383. * @apiGroup Album
  384. * @apiPermission none
  385. * @apiVersion 0.1.0
  386. * @apiParam {int} [goods_id] 商品id
  387. * @apiParam {int} [store_id] 商户id 模拟值为0
  388. * @apiSuccessExample {json} Success-Response:
  389. * HTTP/1.1 200 OK
  390. * {
  391. * "status": true,
  392. * "status_code": 0,
  393. * "message": "",
  394. * "data": {
  395. * "id": 3,
  396. * "store_id": 0,
  397. * "cat_id": 9,
  398. * "specifications_img": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  399. * "style": "纯白",
  400. * "sort": 11,
  401. * "cover_pic": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  402. * "hot_cake": 1,
  403. * "news": 1,
  404. * "detail": "<p>达达娃达娃达娃大青蛙大全大是大非如果v率v恶策但是许多女性的项目部的不行么法可没副本副本副本么方便</p>",
  405. * "created_at": "2018-05-19 15:09:04",
  406. * "updated_at": "2018-05-19 15:09:04",
  407. * "deleted_at": null,
  408. * "install_img": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  409. * "name": "简约",
  410. * "detail_pic": ""
  411. * "price": "",
  412. * "price_default": "",
  413. * "mobile": "",
  414. * "address": "",
  415. * "realname": "",
  416. * "is_favorite": 1,
  417. * "favorite_id": 4
  418. * }
  419. * }
  420. * @apiErrorExample {json} Error-Response:
  421. * HTTP/1.1 400 Bad Request
  422. * {
  423. * "state": false,
  424. * "code": 1000,
  425. * "message": "传入参数不正确",
  426. * "data": null or []
  427. * }
  428. * 可能出现的错误代码:
  429. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  430. */
  431. public function albumGoodsDetail(Request $request)
  432. {
  433. // $userAuth = AlbumXyxUserModel::find(1);
  434. $userAuth = Auth('api')->user();
  435. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  436. $validator = Validator::make($request->all(), [
  437. 'goods_id' => 'required',
  438. 'store_id' => 'required',
  439. ],[
  440. 'goods_id.required'=>'缺少商品参数',
  441. 'store_id_id.required'=>'缺少STORE参数',
  442. ]);
  443. if ($validator->fails()) {
  444. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  445. }
  446. $goods_id = request('goods_id');
  447. $store_id = request('store_id');
  448. $select_info = ['goods_id'=>$goods_id];
  449. if($userAuth->up_agent_id!=0){
  450. $add_record['agent_id'] = $userAuth->up_agent_id;
  451. $add_record['open_id'] = $userAuth->open_id;
  452. $add_record['action'] = 3;
  453. $add_record['store_id'] = $store_id;
  454. $add_record['detail'] = json_encode($select_info);
  455. AlbumWatchRecord::create($add_record);
  456. $user_agent = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  457. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  458. try{
  459. $this->sendLogsMessage($store_id,$agent->open_id,3,$userAuth->username);
  460. }catch (\Exception $e){
  461. }
  462. }
  463. $ma = AlbumManufacturerModel::where('store_id',$store_id)->first();
  464. $goods = AlbumProductModel::where([['id',$goods_id],['store_id',$store_id]])->first();
  465. $address = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  466. if(!empty($goods) && $userAuth->is_dealer != 1){
  467. $price = DB::table('album_product_price')->where([['agent_id',$userAuth->up_agent_id],['store_id',$store_id],['product_id',$goods['id']]])->first();
  468. if(!empty($price)){
  469. $goods->price = $price->price;
  470. }else{
  471. $goods->price = '';
  472. }
  473. $check_favorite = AlbumFavoriteModel::where([['user_id',$userAuth->id],['store_id',$store_id],['product_id',$goods['id']]])->first();
  474. //dd($check_favorite);
  475. if($check_favorite){
  476. $goods->is_favorite = 1;
  477. $goods->favorite_id = $check_favorite->id;
  478. }else{
  479. $goods->is_favorite = 0;
  480. $goods->favorite_id = '';
  481. }
  482. }elseif(!empty($goods) && $userAuth->is_dealer == 1){
  483. $agent_id = $address['id'];
  484. $price = DB::table('album_product_price')->where([['agent_id',$agent_id],['store_id',$store_id],['product_id',$goods['id']]])->first();
  485. if(!empty($price)){
  486. $goods->price = $price->price;
  487. }else{
  488. $goods->price = '';
  489. }
  490. $goods->is_favorite = 0;
  491. $goods->favorite_id = '';
  492. }
  493. if($userAuth->up_agent_id == 0 || !$address){
  494. $goods->price = '';
  495. $goods->mobile = $ma->phone;
  496. $goods->address = $ma->address;
  497. $goods->realname = $ma->name;
  498. $goods->avatar = $ma->avatar;
  499. }else{
  500. $agent = AlbumUserModel::where('id',$address->user_id)->first();
  501. $goods->mobile = $address->phone;
  502. $goods->address = $address->address;
  503. $goods->realname = $address->name;
  504. $goods->avatar = $agent->avatar;
  505. }
  506. return $this->api($goods);
  507. }
  508. /**
  509. * @api {get} /api/album/favorite_list 收藏列表(favorite_list)
  510. * @apiDescription 收藏列表(favorite_list)
  511. * @apiGroup Album
  512. * @apiPermission none
  513. * @apiVersion 0.1.0
  514. * @apiParam {int} [store_id] 商户id 模拟值为0
  515. * @apiSuccessExample {json} Success-Response:
  516. * HTTP/1.1 200 OK
  517. * {
  518. * "status": true,
  519. * "status_code": 0,
  520. * "message": "",
  521. * "data": [
  522. * {
  523. * "id": 4,
  524. * "user_id": 1,
  525. * "deleted_at": null,
  526. * "created_at": "2018-10-29 15:51:47",
  527. * "updated_at": "2018-10-29 15:53:15",
  528. * "product_id": 3,
  529. * "store_id": 0,
  530. * "goods": {
  531. * "id": 3,
  532. * "store_id": 0,
  533. * "cat_id": 9,
  534. * "specifications_img": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  535. * "style": "纯白",
  536. * "sort": 11,
  537. * "cover_pic": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  538. * "hot_cake": 1,
  539. * "news": 1,
  540. * "detail": "<p>达达娃达娃达娃大青蛙大全大是大非如果v率v恶策但是许多女性的项目部的不行么法可没副本副本副本么方便</p>",
  541. * "created_at": "2018-05-19 15:09:04",
  542. * "updated_at": "2018-05-19 15:09:04",
  543. * "deleted_at": null,
  544. * "install_img": "http://admin.xcx.com/upload/images/20180519/02f2dbe0e1046d7cea8b3b52d5642fb8.jpg",
  545. * "name": "简约",
  546. * "detail_pic": ""
  547. * }
  548. * }
  549. * ]
  550. * }
  551. * @apiErrorExample {json} Error-Response:
  552. * HTTP/1.1 400 Bad Request
  553. * {
  554. * "state": false,
  555. * "code": 1000,
  556. * "message": "传入参数不正确",
  557. * "data": null or []
  558. * }
  559. * 可能出现的错误代码:
  560. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  561. */
  562. public function albumFavoriteList(Request $request)
  563. {
  564. $userAuth = Auth('api')->user();
  565. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  566. $validator = Validator::make($request->all(), [
  567. 'store_id' => 'required',
  568. ],[
  569. 'store_id.required'=>'缺少商户参数',
  570. ]);
  571. if ($validator->fails()) {
  572. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  573. }
  574. $data = $request->input();
  575. $favorite = AlbumFavoriteModel::where([['user_id',$userAuth->id],['store_id',$data['store_id']]])->get();
  576. foreach ($favorite as $key=>$val) {
  577. $favorite[$key]['goods'] = AlbumProductModel::where([['id',$val['product_id']],['store_id',$data['store_id']]])->first();
  578. }
  579. return $this->api($favorite);
  580. }
  581. /**
  582. * @api {post} /api/album/get-watch-recored 查看客户浏览情况(get-watch-recored)
  583. * @apiDescription 查看客户浏览情况(get-watch-recored)
  584. * @apiGroup Album
  585. * @apiPermission none
  586. * @apiVersion 0.1.0
  587. * @apiParam {int} [store_id] 商户id 模拟值为0
  588. * @apiParam {int} [type] 查询方式 1 按用户 2 按时间 3 按类型
  589. * @apiParam {int} [user_id] user_id
  590. * @apiParam {int} [action] 行为类型 1 收藏 2查看类目 3查看商品 4登陆 5点击图片 6 点击导航 7一键拨号
  591. * @apiParam {int} [start_time] 查询开始时间
  592. * @apiParam {int} [end_time] 查询结束时间
  593. * @apiSuccessExample {json} Success-Response:
  594. * HTTP/1.1 200 OK
  595. * {
  596. * "status": true,
  597. * "status_code": 0,
  598. * "message": "",
  599. * "data": [
  600. * "type1":{ 1 收藏 2查看类目 3查看商品 4登陆 5点击图片 6 点击导航 7一键拨号
  601. * "time": 2018-1-1 10:10:12,
  602. * "action": '做了什么什么都做',
  603. * "user": [
  604. * "username":"王小贱",
  605. * "avatar":"awdawdawdawdawdawd",头像
  606. * "phone":"1123123123123"
  607. * ]
  608. * }
  609. * ...
  610. * "count":6;
  611. * ]
  612. * }
  613. * @apiErrorExample {json} Error-Response:
  614. * HTTP/1.1 400 Bad Request
  615. * {
  616. * "state": false,
  617. * "code": 1000,
  618. * "message": "传入参数不正确",
  619. * "data": null or []
  620. * }
  621. * 可能出现的错误代码:
  622. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  623. */
  624. public function albumGetWatchRecord(Request $request)
  625. {
  626. $userAuth = Auth('api')->user();
  627. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  628. $validator = Validator::make($request->all(), [
  629. 'store_id' => 'required',
  630. 'type'=>'required'
  631. ],[
  632. 'store_id.required'=>'缺少商户参数',
  633. 'type.required'=>'缺少类型参数',
  634. ]);
  635. if ($validator->fails()) {
  636. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  637. }
  638. $type = $request->input('type');
  639. $store_id = $request->input('store_id');
  640. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  641. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  642. $query = AlbumWatchRecord::where([['agent_id',$user_agent->id],['store_id',$store_id]]);
  643. switch ($type){
  644. case '1':
  645. if(!$request->input('user_id')) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!');
  646. $user = AlbumUserModel::find(request('user_id'));
  647. $query = $query->where('open_id',$user->open_id);
  648. break;
  649. case '2':
  650. if(!$request->input('start_time')||!$request->input('end_time')) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!');
  651. $start_time = date('Y-m-d H:i:s',$request->input('start_time'));
  652. $end_time = date('Y-m-d H:i:s',$request->input('end_time'));//dd($start_time.';'.$end_time);
  653. $query = $query->where([['created_at','>=',$start_time],['created_at','<=',$end_time]]);
  654. break;
  655. case '3':
  656. if(!$request->input('action')) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!');
  657. $query = $query->where([['action',$request->input('action')]]);
  658. break;
  659. }
  660. $res = $query->get();
  661. $action = '';
  662. foreach($res as $key=>$val){
  663. \Log::info('action:'.$val->action.',id:'.$val->id.';end');
  664. if($val->action==4){
  665. $action = '进入我的画册';
  666. }elseif($val->action == 1||$val->action ==3){
  667. $goods_data = json_decode($val->detail,true);
  668. $goods_id = $goods_data['goods_id'];
  669. $goods = AlbumProductModel::find($goods_id);
  670. $cat = AlbumCatModel::find($goods->cat_id);
  671. $style = AlbumProductStyleModel::find($goods->style);
  672. $val->action == 1? $act = '收藏':$act = '查看';if(empty($style)||empty($cat)) continue;
  673. $action = $act.'了您的'.$cat->name.'分类下的'.$style->name.'风格的商品 '.$goods->name;
  674. }elseif($val->action == 2){
  675. //dd(111);
  676. $cat_data = json_decode($val->detail,true);
  677. if($cat_data['cat_id']=="undefined"||$cat_data['style']=="undefined"||!$cat_data['style']||!$cat_data['cat_id']) continue;
  678. $cat = AlbumCatModel::find($cat_data['cat_id']);
  679. $style = AlbumProductStyleModel::find($cat_data['style']);
  680. if(empty($style)||empty($cat)) continue;
  681. $action = '浏览了您的 '.$cat->name.'分类下的 '.$style->name.'风格的商品列表';
  682. }elseif($val->action==5){
  683. $goods_data = json_decode($val->detail,true);
  684. $goods_id = $goods_data['goods_id'];
  685. $goods = AlbumProductModel::find($goods_id);
  686. $cat = AlbumCatModel::find($goods->cat_id);
  687. $style = AlbumProductStyleModel::find($goods->style);if(empty($style)||empty($cat)) continue;
  688. $action = '点击了您的'.$cat->name.'分类下的'.$style->name.'风格的商品 '.$goods->name.'的'.$goods_data['detail'];
  689. }elseif($val->action==6){
  690. $detail = json_decode($val->detail,true);
  691. $address = $detail['detail'];
  692. $action = '查看了您的地址'.$address.',并且点击了导航';
  693. }elseif($val->action==7){
  694. $detail = json_decode($val->detail,true);
  695. $phone = $detail['detail'];
  696. $action = '查看了您的电话'.$phone.',并且点击了拨号';
  697. }
  698. $user = AlbumUserModel::where([['open_id',$val->open_id],['store_id',$val->store_id]])->select(['username','avatar','phone'])->first();
  699. // dd(111);
  700. $data_res[] = [
  701. 'time'=>$val->created_at,
  702. 'action'=>$action,
  703. 'user'=>$user
  704. ];
  705. }
  706. return $this->api(compact('data_res'));
  707. }
  708. /**
  709. * @api {post} /api/album/get_data_goods 获取访问数据(get_data_goods)
  710. * @apiDescription 获取访问数据(get_data_goods)
  711. * @apiGroup Album
  712. * @apiPermission 需要登录
  713. * @apiVersion 0.1.0
  714. * @apiParam {int} [store_id] 商户id 模拟值为0
  715. * @apiSuccessExample {json} Success-Response:
  716. * HTTP/1.1 200 OK
  717. * {
  718. * "status": true,
  719. * "status_code": 0,
  720. * "message": "",
  721. * "data": [
  722. * {
  723. * "1": [
  724. * 'name':'asdawd',
  725. * 'point':'asdawd',
  726. * 'num':'1',
  727. * ],
  728. * }
  729. * ]
  730. * }
  731. * @apiErrorExample {json} Error-Response:
  732. * HTTP/1.1 400 Bad Request
  733. * {
  734. * "state": false,
  735. * "code": 1000,
  736. * "message": "传入参数不正确",
  737. * "data": null or []
  738. * }
  739. * 可能出现的错误代码:
  740. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  741. */
  742. public function albumGetDataGoods(Request $request)
  743. {
  744. $userAuth = Auth('api')->user();
  745. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  746. $validator = Validator::make($request->all(), [
  747. 'store_id' => 'required'
  748. ],[
  749. 'store_id.required'=>'缺少商户参数'
  750. ]);
  751. if ($validator->fails()) {
  752. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  753. }
  754. $store_id = $request->input('store_id');
  755. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  756. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  757. $res = AlbumWatchRecord::where([['agent_id',$user_agent->id],['store_id',$store_id],['action',3]])->get();
  758. $arr = array();
  759. $total = 0;
  760. foreach($res as $key=>$val){
  761. $goods_data = json_decode($val->detail,true);
  762. $goods_id = $goods_data['goods_id'];
  763. $goods = AlbumProductModel::find($goods_id);
  764. if(isset($arr[$goods->id])){
  765. $arr[$goods->id]['num']++;
  766. }else{
  767. $arr[$goods->id]['num']=1;
  768. $arr[$goods->id]['name']=$goods->name;
  769. }
  770. $total++;
  771. }
  772. foreach($arr as $key=>$val){
  773. $arr[$key]['point'] = ($val['num']/$total*100).'%';
  774. }
  775. return $this->api(compact('arr'));
  776. }
  777. /**
  778. * @api {post} /api/album/get_data_cat 获取分类访问数据(get_data_cat)
  779. * @apiDescription 获取分类访问数据(get_data_cat)
  780. * @apiGroup Album
  781. * @apiPermission 需要登录
  782. * @apiVersion 0.1.0
  783. * @apiParam {int} [store_id] 商户id 模拟值为0
  784. * @apiSuccessExample {json} Success-Response:
  785. * HTTP/1.1 200 OK
  786. * {
  787. * "status": true,
  788. * "status_code": 0,
  789. * "message": "",
  790. * "data": [
  791. * {
  792. * "1": [
  793. * 'name':'asdawd',
  794. * 'point':'asdawd',
  795. * 'num':'1',
  796. * ],
  797. * }
  798. * ]
  799. * }
  800. * @apiErrorExample {json} Error-Response:
  801. * HTTP/1.1 400 Bad Request
  802. * {
  803. * "state": false,
  804. * "code": 1000,
  805. * "message": "传入参数不正确",
  806. * "data": null or []
  807. * }
  808. * 可能出现的错误代码:
  809. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  810. */
  811. public function albumGetDataCat(Request $request)
  812. {
  813. $userAuth = Auth('api')->user();
  814. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  815. $validator = Validator::make($request->all(), [
  816. 'store_id' => 'required',
  817. 'style_id' => 'required'
  818. ],[
  819. 'store_id.required'=>'缺少商户参数',
  820. 'style_id.required'=>'缺少风格参数',
  821. ]);
  822. if ($validator->fails()) {
  823. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  824. }
  825. $store_id = $request->input('store_id');
  826. $style_id = $request->input('style_id');
  827. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  828. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  829. $cat = AlbumCatModel::where('store_id',$store_id)->get(['name','id'])->toArray();
  830. $total = 0;
  831. foreach ($cat as $key=>$val){
  832. $count = CustomerCatRecordModel::where([['agent_id',$user_agent->id],['store_id',$store_id],['style_id',$style_id],['cat_id',$val['id']]])->count();
  833. $total += $count;
  834. $cat[$key]['num'] = $count;
  835. }
  836. foreach($cat as $key=>$val){
  837. if($val['num']==0||$total==0){
  838. $cat[$key]['point']=0;
  839. }else{
  840. $cat[$key]['point'] = ($val['num']/$total*100).'%';
  841. }
  842. }
  843. return $this->api(compact('cat'));
  844. }
  845. /**
  846. * @api {post} /api/album/get-cart-of-watch 客户浏览情况概览(get-cart-of-watch)
  847. * @apiDescription 客户浏览情况概览(get-cart-of-watch)
  848. * @apiGroup Album
  849. * @apiPermission none
  850. * @apiVersion 0.1.0
  851. * @apiParam {int} [store_id] 商户id 模拟值为0
  852. * @apiSuccessExample {json} Success-Response:
  853. * HTTP/1.1 200 OK
  854. * {
  855. * "status": true,
  856. * "status_code": 0,
  857. * "message": "",
  858. * "data": [
  859. * "user": {
  860. * "username":"王小贱",
  861. * "avatar":"awdawdawdawdawdawd",头像
  862. * "phone":"1123123123123"
  863. * "open_id":"1123123123123"
  864. * }
  865. * "count":6,
  866. * "action":[
  867. * "type1":1,
  868. * "type2":1,
  869. * "type3":1,
  870. * "type4":1,
  871. * "type5":1,
  872. * "type6":1,
  873. * "type7":1,
  874. * ]
  875. * ]
  876. * }
  877. * @apiErrorExample {json} Error-Response:
  878. * HTTP/1.1 400 Bad Request
  879. * {
  880. * "state": false,
  881. * "code": 1000,
  882. * "message": "传入参数不正确",
  883. * "data": null or []
  884. * }
  885. * 可能出现的错误代码:
  886. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  887. */
  888. public function albumGetCartOfWatch(Request $request)
  889. {
  890. $userAuth = Auth('api')->user();
  891. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  892. $validator = Validator::make($request->all(), [
  893. 'store_id' => 'required',
  894. 'end' => 'required',
  895. 'start' => 'required',
  896. ],[
  897. 'store_id.required'=>'缺少商户参数',
  898. 'end.required'=>'缺少结束时间参数',
  899. 'start.required'=>'缺少开始时间参数',
  900. ]);
  901. if ($validator->fails()) {
  902. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  903. }
  904. $store_id = $request->input('store_id');
  905. $end = $request->input('end');
  906. $start = $request->input('start');
  907. $end= date('Y-m-d H:i:s',$end);
  908. $start= date('Y-m-d H:i:s',$start);
  909. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  910. // if($userAuth->up_agent_id==0) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未选择经销商!', $validator->messages());
  911. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  912. $count_user = AlbumWatchRecord::where([['agent_id',$user_agent->id],['store_id',$store_id],['updated_at','>=',$start],['updated_at','<=',$end]])->orderBy('id','DESC')->groupBy('open_id')->get();
  913. $users = Array();
  914. if($count_user){
  915. foreach($count_user as $key=>$val){
  916. $user = AlbumUserModel::where([['open_id',$val->open_id],['store_id',$val->store_id]])->select(['id','username','avatar','phone','open_id'])->first();
  917. $customer = CustomerDetailsModel::where([['store_id',$store_id],['open_id',$val->open_id],['agent_id',$user_agent->id]])->first(['purpose_level','comment','tips']);
  918. $user->time = $val->updated_at;
  919. if($customer){
  920. $user->purpose_level = $customer->purpose_level;
  921. $user->comment = $customer->comment;
  922. $user->tips = $customer->tips;
  923. }else{
  924. $user->purpose_level = 0;
  925. $user->comment = 0;
  926. $user->tips = 0;
  927. }
  928. $users[] = $user;
  929. }
  930. }
  931. $count = count($users);
  932. $action = Array();
  933. for($i=1;$i<=7;$i++){
  934. $res = AlbumWatchRecord::where([['agent_id',$user_agent->id],['store_id',$store_id],['action',$i]])->get();
  935. $action['type'.$i] = count($res);
  936. }
  937. return $this->api(compact('count','users','action'));
  938. }
  939. /**
  940. * @api {get} /api/album/checklogin 登陆应用(checklogin)
  941. * @apiDescription 登陆应用(checklogin)
  942. * @apiGroup Album
  943. * @apiPermission none
  944. * @apiVersion 0.1.0
  945. * @apiParam {int} [store_id] 商户id 模拟值为0
  946. * @apiSuccessExample {json} Success-Response:
  947. * HTTP/1.1 200 OK
  948. * {
  949. * "status": true,
  950. * "status_code": 0,
  951. * "message": "",
  952. * "data": {
  953. *
  954. * }
  955. * }
  956. * @apiErrorExample {json} Error-Response:
  957. * HTTP/1.1 400 Bad Request
  958. * {
  959. * "state": false,
  960. * "code": 1000,
  961. * "message": "传入参数不正确",
  962. * "data": null or []
  963. * }
  964. * 可能出现的错误代码:
  965. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  966. */
  967. public function albumchecklogin(Request $request)
  968. {
  969. $validator = Validator::make($request->all(), [
  970. 'store_id' => 'required',
  971. ],[
  972. 'store_id.required'=>'缺少商户参数',
  973. ]);
  974. if ($validator->fails()) {
  975. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  976. }
  977. $data = $request->input();
  978. $favorite = AlbumManufacturerModel::where([['store_id',$data['store_id']]])->first();
  979. $conf = $favorite->redirectapp;
  980. return $this->api($conf);
  981. }
  982. /**
  983. * @api {get} /api/album/favorite_del 收藏删除(favorite_del)
  984. * @apiDescription 收藏删除(favorite_del)
  985. * @apiGroup Album
  986. * @apiPermission none
  987. * @apiVersion 0.1.0
  988. * @apiParam {int} [store_id] 商户id 模拟值为0
  989. * @apiParam {int} [favorite_id] 收藏id
  990. * @apiSuccessExample {json} Success-Response:
  991. * HTTP/1.1 200 OK
  992. * {
  993. * "status": true,
  994. * "status_code": 0,
  995. * "message": "",
  996. * "data":[]//返回状态
  997. * }
  998. * @apiErrorExample {json} Error-Response:
  999. * HTTP/1.1 400 Bad Request
  1000. * {
  1001. * "state": false,
  1002. * "code": 1000,
  1003. * "message": "传入参数不正确",
  1004. * "data": null or []
  1005. * }
  1006. * 可能出现的错误代码:
  1007. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1008. */
  1009. public function albumFavoriteDel(Request $request)
  1010. {
  1011. //$userAuth = AlbumXyxUserModel::find(1);
  1012. $userAuth = Auth('api')->user();
  1013. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1014. $validator = Validator::make($request->all(), [
  1015. 'store_id' => 'required',
  1016. 'favorite_id' => 'required',
  1017. ],[
  1018. 'store_id.required'=>'缺少商户参数',
  1019. 'favorite_id.required'=>'缺少收藏参数',
  1020. ]);
  1021. if ($validator->fails()) {
  1022. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1023. }
  1024. $data = $request->input();
  1025. $favorite = AlbumFavoriteModel::find($data['favorite_id']);
  1026. if(!$favorite){
  1027. $d = [
  1028. 'code' =>1,
  1029. 'msg' =>'删除失败,或者该收藏已移除',
  1030. ];
  1031. } else {
  1032. $res = $favorite->delete();
  1033. if($res){
  1034. $d = [
  1035. 'code' =>1,
  1036. 'msg' =>'删除成功',
  1037. ];
  1038. } else {
  1039. $d = [
  1040. 'code' =>1,
  1041. 'msg' =>'删除失败,或者该收藏已移除',
  1042. ];
  1043. }
  1044. }
  1045. return $this->api($d);
  1046. }
  1047. /**
  1048. * @api {get} /api/album/save_form_id 添加Form_id(save_form_id)
  1049. * @apiDescription 添加Form_id(save_form_id)
  1050. * @apiGroup Album
  1051. * @apiPermission none
  1052. * @apiVersion 0.1.0
  1053. * @apiParam {int} [store_id] 商户id
  1054. * @apiParam {int} [form_id] form_id
  1055. * @apiSuccessExample {json} Success-Response:
  1056. * HTTP/1.1 200 OK
  1057. * {
  1058. * "status": true,
  1059. * "status_code": 0,
  1060. * "message": "",
  1061. * "data": []//返回信息
  1062. * }
  1063. * @apiErrorExample {json} Error-Response:
  1064. * HTTP/1.1 400 Bad Request
  1065. * {
  1066. * "state": false,
  1067. * "code": 1000,
  1068. * "message": "传入参数不正确",
  1069. * "data": null or []
  1070. * }
  1071. * 可能出现的错误代码:
  1072. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1073. */
  1074. public function albumSaveFormId(Request $request)
  1075. {
  1076. $userAuth = Auth('api')->user();
  1077. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1078. $validator = Validator::make($request->all(), [
  1079. 'store_id' => 'required',
  1080. 'form_id' => 'required',
  1081. ],[
  1082. 'store_id.required'=>'缺少商户参数',
  1083. 'form_id.required'=>'缺少FormId',
  1084. ]);
  1085. if ($validator->fails()) {
  1086. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1087. }
  1088. $data = $request->input();
  1089. $data['open_id'] = $userAuth->open_id;
  1090. $res = AlbumFormId::create($data);
  1091. if($res){
  1092. $d = [
  1093. 'code' =>0,
  1094. 'msg' =>'success',
  1095. ];
  1096. }else{
  1097. $d = [
  1098. 'code' =>1,
  1099. 'msg' =>'error',
  1100. ];
  1101. }
  1102. return $this->api($d);
  1103. }
  1104. /**
  1105. * @api {get} /api/album/add_favorite 添加收藏(add_favorite)
  1106. * @apiDescription 添加收藏(favorite_list)
  1107. * @apiGroup Album
  1108. * @apiPermission none
  1109. * @apiVersion 0.1.0
  1110. * @apiParam {int} [store_id] 商户id
  1111. * @apiParam {int} [product_id] 商品id
  1112. * @apiSuccessExample {json} Success-Response:
  1113. * HTTP/1.1 200 OK
  1114. * {
  1115. * "status": true,
  1116. * "status_code": 0,
  1117. * "message": "",
  1118. * "data": []//返回信息
  1119. * }
  1120. * @apiErrorExample {json} Error-Response:
  1121. * HTTP/1.1 400 Bad Request
  1122. * {
  1123. * "state": false,
  1124. * "code": 1000,
  1125. * "message": "传入参数不正确",
  1126. * "data": null or []
  1127. * }
  1128. * 可能出现的错误代码:
  1129. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1130. */
  1131. public function albumAddFavorite(Request $request)
  1132. {
  1133. $userAuth = Auth('api')->user();
  1134. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1135. $validator = Validator::make($request->all(), [
  1136. 'store_id' => 'required',
  1137. 'product_id' => 'required',
  1138. ],[
  1139. 'store_id.required'=>'缺少商户参数',
  1140. 'product_id.required'=>'缺少商品参数',
  1141. ]);
  1142. if ($validator->fails()) {
  1143. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1144. }
  1145. $data = $request->input();
  1146. $select_info = ['goods_id'=>$data['product_id']];
  1147. if($userAuth->up_agent_id!=0){
  1148. $add_record['agent_id'] = $userAuth->up_agent_id;
  1149. $add_record['open_id'] = $userAuth->open_id;
  1150. $add_record['action'] = 1;
  1151. $add_record['store_id'] = $data['store_id'];
  1152. $add_record['detail'] = json_encode($select_info);
  1153. $user_agent = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  1154. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  1155. try{
  1156. $this->sendLogsMessage($data['store_id'],$agent->open_id,1,$userAuth->username);
  1157. }catch (\Exception $e){
  1158. }
  1159. AlbumWatchRecord::create($add_record);
  1160. }
  1161. $check_user = AlbumUserModel::find($userAuth->id);
  1162. $check_f = AlbumFavoriteModel::where([['user_id',$userAuth->id],['product_id',$data['product_id']]])->first();
  1163. if(!$check_user||$check_f){
  1164. $d = [
  1165. 'code' =>1,
  1166. 'msg' =>'该用户不存在或者已添加',
  1167. ];
  1168. return $this->api($d);
  1169. }
  1170. $data['user_id'] = $userAuth->id;
  1171. $res = AlbumFavoriteModel::create($data);
  1172. if($res){
  1173. $d = [
  1174. 'code' =>0,
  1175. 'id'=>$res->id,
  1176. 'msg' =>'success',
  1177. ];
  1178. }else{
  1179. $d = [
  1180. 'code' =>1,
  1181. 'msg' =>'error',
  1182. ];
  1183. }
  1184. return $this->api($d);
  1185. }
  1186. /**
  1187. * @api {get} /api/album/cat 分类列表(cat)
  1188. * @apiDescription 画册分类(cat)
  1189. * @apiGroup Album
  1190. * @apiPermission none
  1191. * @apiVersion 0.1.0
  1192. * @apiParam {int} [parent_id] 一级菜单传0 二级菜单的id从一级菜单里面获取
  1193. * @apiParam {int} [store_id] 商户id 模拟值为0
  1194. * @apiSuccessExample {json} Success-Response:
  1195. * HTTP/1.1 200 OK
  1196. * {
  1197. * "status": true,
  1198. * "status_code": 0,
  1199. * "message": "",
  1200. * "data": [
  1201. * {
  1202. * "id": 9,
  1203. * "name": "圣地亚哥",
  1204. * "parent_id": 0,
  1205. * "level": "00",
  1206. * "pic_url": "http://admin.xcx.com/upload/images/20180518/af6cc8fd71241744ccd638afc6ac25f2.png",
  1207. * "created_at": "2018-05-19 14:55:48",
  1208. * "updated_at": "2018-05-19 14:55:48",
  1209. * "store_id": 0,
  1210. * "deleted_at": null,
  1211. * "sort": 0
  1212. * }
  1213. * ]
  1214. * }
  1215. * @apiErrorExample {json} Error-Response:
  1216. * HTTP/1.1 400 Bad Request
  1217. * {
  1218. * "state": false,
  1219. * "code": 1000,
  1220. * "message": "传入参数不正确",
  1221. * "data": null or []
  1222. * }
  1223. * 可能出现的错误代码:
  1224. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1225. */
  1226. public function albumCat(Request $request)
  1227. {
  1228. $validator = Validator::make($request->all(), [
  1229. 'store_id' => 'required',
  1230. ],[
  1231. 'store_id.required'=>'缺少商户参数',
  1232. ]);
  1233. if ($validator->fails()) {
  1234. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1235. }
  1236. $cat_id = 0;
  1237. $store_id = request('store_id');
  1238. $cats = AlbumCatModel::where([['parent_id',$cat_id],['store_id',$store_id]])->orderByDesc('sort')->get();
  1239. return $this->api($cats);
  1240. }
  1241. /**
  1242. * @api {get} /api/album/style 属性列表(style)
  1243. * @apiDescription 分类样式(style)
  1244. * @apiGroup Album
  1245. * @apiPermission none
  1246. * @apiVersion 0.1.0
  1247. * @apiParam {int} [store_id] 商户id 模拟值为0
  1248. * @apiSuccessExample {json} Success-Response:
  1249. * HTTP/1.1 200 OK
  1250. * {
  1251. * "status": true,
  1252. * "status_code": 0,
  1253. * "message": "",
  1254. * "data": [
  1255. * {
  1256. * "id": 2,
  1257. * "name": "1111",
  1258. * "deleted_at": null,
  1259. * "created_at": "2018-06-05 17:35:08",
  1260. * "updated_at": "2018-06-05 17:35:08",
  1261. * "store_id": 0
  1262. * }
  1263. * ]
  1264. * }
  1265. * @apiErrorExample {json} Error-Response:
  1266. * HTTP/1.1 400 Bad Request
  1267. * {
  1268. * "state": false,
  1269. * "code": 1000,
  1270. * "message": "传入参数不正确",
  1271. * "data": null or []
  1272. * }
  1273. * 可能出现的错误代码:
  1274. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1275. */
  1276. public function albumStyle(Request $request)
  1277. {
  1278. $validator = Validator::make($request->all(), [
  1279. 'store_id' => 'required',
  1280. ],[
  1281. 'store_id.required'=>'缺少商户参数',
  1282. ]);
  1283. if ($validator->fails()) {
  1284. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1285. }
  1286. $store_id = request('store_id');
  1287. $style = AlbumProductStyleModel::where([['store_id',$store_id]])->orderByDesc('id')->get();
  1288. return $this->api($style);
  1289. }
  1290. /**
  1291. * @api {get} /api/album/set-price 经销商修改价格(set-price)
  1292. * @apiDescription 画册(set-price)
  1293. * @apiGroup Album
  1294. * @apiPermission none
  1295. * @apiVersion 0.1.0
  1296. * @apiParam {int} [goods_id] 商品id
  1297. * @apiParam {int} [store_id] 商户id 模拟值为0
  1298. * @apiParam {double} [price] 价格
  1299. * @apiSuccessExample {json} Success-Response:
  1300. * HTTP/1.1 200 OK
  1301. * {
  1302. * "status": true,
  1303. * "status_code": 0,
  1304. * "message": "",
  1305. * "data":[
  1306. * "msg":返回信息,
  1307. * "data":修改后价格
  1308. * ]
  1309. * }
  1310. * @apiErrorExample {json} Error-Response:
  1311. * HTTP/1.1 400 Bad Request
  1312. * {
  1313. * "state": false,
  1314. * "code": 1000,
  1315. * "message": "传入参数不正确",
  1316. * "data": null or []
  1317. * }
  1318. * 可能出现的错误代码:
  1319. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1320. */
  1321. public function albumSetPrice(Request $request)
  1322. {
  1323. $userAuth = Auth('api')->user();
  1324. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1325. $agent_id = $user_agent['id'];
  1326. $data = $request->all();
  1327. //print_r($data);
  1328. $save = array();
  1329. $return_data = array();
  1330. $save['price'] = $data['price'];
  1331. $check = AlbumProductPriceModel::where([['agent_id',$agent_id],['store_id',$data['store_id']],['product_id',$data['goods_id']]])->first();
  1332. if(empty($check)){
  1333. $save['agent_id'] = $agent_id;
  1334. $save['store_id'] = $data['store_id'];
  1335. $save['product_id'] = $data['goods_id'];
  1336. $res = AlbumProductPriceModel::create($save);
  1337. }else{
  1338. $res = AlbumProductPriceModel::where([['agent_id',$agent_id],['store_id',$data['store_id']],['product_id',$data['goods_id']]])->update($save);
  1339. }
  1340. if($res){
  1341. $return['msg'] = 'success';
  1342. $return_data['price'] = $data['price'];
  1343. } else {
  1344. $return['msg'] = 'error';
  1345. }
  1346. $return['data'] = $return_data;
  1347. return $this->api($return);
  1348. }
  1349. /**
  1350. * @api {post} /api/album/set-phone 获取用户号码(set-phone)
  1351. * @apiDescription 获取用户号码(set-phone)
  1352. * @apiGroup Album
  1353. * @apiPermission none
  1354. * @apiVersion 0.1.0
  1355. * @apiParam {int} [store_id] 商户id 模拟值为0
  1356. * @apiParam {string} [code] code
  1357. * @apiParam {string} [iv] 偏移量
  1358. * @apiParam {string} [encrypted] 加密参数
  1359. * @apiSuccessExample {json} Success-Response:
  1360. * HTTP/1.1 200 OK
  1361. * {
  1362. * "status": true,
  1363. * "status_code": 0,
  1364. * "message": "",
  1365. * "data":[
  1366. * "msg":返回信息,
  1367. * ]
  1368. * }
  1369. * @apiErrorExample {json} Error-Response:
  1370. * HTTP/1.1 400 Bad Request
  1371. * {
  1372. * "state": false,
  1373. * "code": 1000,
  1374. * "message": "传入参数不正确",
  1375. * "data": null or []
  1376. * }
  1377. * 可能出现的错误代码:
  1378. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1379. */
  1380. public function albumSavePhone(Request $request)
  1381. {
  1382. $userAuth = Auth('api')->user();
  1383. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1384. $validator = Validator::make($request->all(), [
  1385. 'store_id' => 'required',
  1386. 'code' => 'required',
  1387. 'iv' => 'required',
  1388. 'encrypted' => 'required',
  1389. ],[
  1390. 'store_id.required'=>'缺少商户参数',
  1391. 'code.required'=>'缺少code参数',
  1392. 'iv.required'=>'缺少偏移量参数',
  1393. 'encrypted.required'=>'缺少加密参数',
  1394. ]);
  1395. if ($validator->fails()) {
  1396. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1397. }
  1398. $this->wechat_app = AlbumManufacturerModel::where('store_id',$request->input('store_id'))->first();
  1399. $config = [
  1400. 'app_id' => $this->wechat_app->xyx_id,
  1401. 'secret' => $this->wechat_app->xyx_secret,
  1402. // 下面为可选项
  1403. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  1404. 'response_type' => 'array',
  1405. ];
  1406. $app = Factory::miniProgram($config);
  1407. $res = $app->auth->session($request->input('code'));
  1408. if (!$res || empty($res['openid'])) {
  1409. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '获取用户OpenId失败!', $validator->messages());
  1410. }
  1411. $session_key = $res['session_key'];
  1412. $pc = new WXBizDataCrypt($this->wechat_app->xyx_id, $session_key);
  1413. $errCode = $pc->decryptData($request->input('encrypted'), $request->input('iv'), $data);
  1414. if($errCode==0){
  1415. $data = json_decode($data,true);
  1416. $userAuth->phone = $data['phoneNumber'];
  1417. $res = $userAuth->save();
  1418. if($res){
  1419. return $this->api(['msg'=>'success']);
  1420. }else{
  1421. return $this->api(['msg'=>'error']);
  1422. }
  1423. }elseif($errCode == -41003){
  1424. return $this->api(['msg'=>'请求错误,请重试']);
  1425. }
  1426. }
  1427. /**
  1428. * @api {post} /api/album/set-watch 获取用户操作记录(set-watch)
  1429. * @apiDescription 获取用户操作记录(set-watch)
  1430. * @apiGroup Album
  1431. * @apiPermission none
  1432. * @apiVersion 0.1.0
  1433. * @apiParam {int} [store_id] 商户id 模拟值为0
  1434. * @apiParam {string} [detail] 详细
  1435. * @apiParam {string} [type] 5点击图片 6 点击导航 7一键拨号
  1436. * @apiSuccessExample {json} Success-Response:
  1437. * HTTP/1.1 200 OK
  1438. * {
  1439. * "status": true,
  1440. * "status_code": 0,
  1441. * "message": "",
  1442. * "data":[
  1443. * "msg":返回信息,
  1444. * ]
  1445. * }
  1446. * @apiErrorExample {json} Error-Response:
  1447. * HTTP/1.1 400 Bad Request
  1448. * {
  1449. * "state": false,
  1450. * "code": 1000,
  1451. * "message": "传入参数不正确",
  1452. * "data": null or []
  1453. * }
  1454. * 可能出现的错误代码:
  1455. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1456. */
  1457. public function albumSetWatch(Request $request)
  1458. {
  1459. $userAuth = Auth('api')->user();
  1460. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1461. $validator = Validator::make($request->all(), [
  1462. 'store_id' => 'required',
  1463. 'type' => 'required',
  1464. 'detail' => 'required',
  1465. ],[
  1466. 'store_id.required'=>'缺少商户参数',
  1467. 'type.required'=>'缺少类型参数',
  1468. 'detail.required'=>'缺少详细参数',
  1469. ]);
  1470. if ($validator->fails()) {
  1471. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1472. }
  1473. $data = $request->input();
  1474. if($data['type']==5&&!empty($data['goods_id'])) {
  1475. $select_info = ['goods_id'=>$data['goods_id'],'detail'=>$data['detail']];
  1476. }elseif($data['type']==5&&empty($data['goods_id'])){
  1477. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '缺少goodsid');
  1478. }else{
  1479. $select_info = ['detail'=>$data['detail']];
  1480. }
  1481. if($userAuth->up_agent_id!=0){
  1482. $add_record['agent_id'] = $userAuth->up_agent_id;
  1483. $add_record['open_id'] = $userAuth->open_id;
  1484. $add_record['action'] = $data['type'];
  1485. $add_record['store_id'] = 1;
  1486. $add_record['detail'] = json_encode($select_info);
  1487. // dd($add_record);
  1488. $res = AlbumWatchRecord::create($add_record);
  1489. $user_agent = AlbumAgentModel::where('id',$userAuth->up_agent_id)->first();
  1490. $agent = AlbumUserModel::where('id',$user_agent->user_id)->first();
  1491. if($res){
  1492. try{
  1493. $this->sendLogsMessage($data['store_id'],$agent->open_id,$data['type'],$userAuth->username);
  1494. }catch (\Exception $e){
  1495. }
  1496. return $this->api(['msg'=>'success']);
  1497. }else{
  1498. return $this->api(['msg'=>'error']);
  1499. }
  1500. }
  1501. }
  1502. /**
  1503. * @param $storeid
  1504. * @param $open_id
  1505. * @param $action 1 收藏 2查看类目 3查看商品 4登陆 5点击图片 6 点击导航 7一键拨号
  1506. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  1507. */
  1508. private function sendLogsMessage($storeid,$open_id,$action,$name)
  1509. {
  1510. $user = AlbumUserModel::where('open_id',$open_id)->first();
  1511. $formId = AlbumFormId::where([['open_id',$open_id]])->first();
  1512. if($user&&$formId){
  1513. $content = '';
  1514. switch ($action){
  1515. case 1:
  1516. $content = '有用户收藏了您的产品';
  1517. break;
  1518. case 2;
  1519. $content = '有用户查看了您的产品列表';
  1520. break;
  1521. case 3;
  1522. $content = '有用户查看了您的商品';
  1523. break;
  1524. case 4;
  1525. $content = '有用户进入了您的画册';
  1526. break;
  1527. case 5;
  1528. $content = '有用户点击了您的某款产品详情';
  1529. break;
  1530. case 6;
  1531. $content = '有用户点击了导航到您的位置';
  1532. break;
  1533. case 7;
  1534. $content = '有用户点击了您的号码';
  1535. break;
  1536. }
  1537. $this->wechat_app = AlbumManufacturerModel::where('store_id',$storeid)->first();
  1538. $config = [
  1539. 'app_id' => $this->wechat_app->app_id,
  1540. 'secret' => $this->wechat_app->app_secret,
  1541. // 下面为可选项
  1542. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  1543. 'response_type' => 'array',
  1544. ];
  1545. $app = Factory::miniProgram($config);
  1546. $res = $app->template_message->send([
  1547. 'touser' => $open_id,
  1548. 'template_id' => '9PDTfnhsRvdNwpPMIcYLL7wQF4SZkB8-9ZhXUfthbmw',
  1549. 'page' => 'pages/logs/logs',
  1550. 'form_id' => $formId->form_id,
  1551. 'data' => [
  1552. 'keyword1' => $name,
  1553. 'keyword2' => $content,
  1554. 'keyword3' => date('Y-m-d H:i'),
  1555. ],
  1556. ]);
  1557. // dd($res);
  1558. if($res){
  1559. $formId->delete();
  1560. }
  1561. }
  1562. }
  1563. /**
  1564. * @api {get} /api/album/add_agent 申请经销商(add_agent)
  1565. * @apiDescription 申请经销商(add_agent)
  1566. * @apiGroup Album
  1567. * @apiPermission none
  1568. * @apiVersion 0.1.0
  1569. * @apiParam {int} [store_id] 商户id
  1570. * @apiParam {int} [realname] 姓名
  1571. * @apiParam {int} [address] 地址
  1572. * @apiParam {double} [lat] lat
  1573. * @apiParam {double} [lon] lon
  1574. * @apiParam {int} [phone] 电话
  1575. * @apiSuccessExample {json} Success-Response:
  1576. * HTTP/1.1 200 OK
  1577. * {
  1578. * "status": true,
  1579. * "status_code": 0,
  1580. * "message": "",
  1581. * "data": []//返回信息
  1582. * }
  1583. * @apiErrorExample {json} Error-Response:
  1584. * HTTP/1.1 400 Bad Request
  1585. * {
  1586. * "state": false,
  1587. * "code": 1000,
  1588. * "message": "传入参数不正确",
  1589. * "data": null or []
  1590. * }
  1591. * 可能出现的错误代码:
  1592. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1593. */
  1594. public function albumAddAgent(Request $request)
  1595. {
  1596. $userAuth = Auth('api')->user();
  1597. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1598. $validator = Validator::make($request->all(), [
  1599. 'realname' => 'required',
  1600. 'address' => 'required',
  1601. 'phone' => 'required',
  1602. 'store_id' => 'required',
  1603. ],[
  1604. 'address.required'=>'缺少地址参数',
  1605. 'real_name.required'=>'缺少信息参数',
  1606. 'phone.required'=>'缺少电话参数',
  1607. 'store_id.required'=>'缺少商户参数',
  1608. ]);
  1609. if ($validator->fails()) {
  1610. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1611. }
  1612. $data = $request->input();
  1613. $url = "https://apis.map.qq.com/ws/geocoder/v1/?address=".$data['address']."&key=U5DBZ-X5VCO-6UUWV-SGPU3-PD5X2-NDFXA";
  1614. $local = $this->curl_get($url);
  1615. $res = json_decode($local,true);
  1616. $agent_check = AlbumAgentModel::where([['store_id',$data['store_id']],['user_id',$userAuth->id]])->first();
  1617. if($agent_check){
  1618. if($agent_check->status==0) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户已提交申请!');
  1619. $agent_check->phone = $data['phone'];
  1620. $agent_check->address = $data['address'];
  1621. $agent_check->lon = $res['result']['location']['lon'];
  1622. $agent_check->lat = $res['result']['location']['lat'];
  1623. $agent_check->realname = $data['realname'];
  1624. if($agent_check->save()) return $this->api([
  1625. 'code' =>0,
  1626. 'msg' =>'success',
  1627. ]);
  1628. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '保存失败!');
  1629. }
  1630. $data['status'] = 0;
  1631. $data['user_id'] = $userAuth->id;
  1632. $data['name'] = $userAuth->username;
  1633. $res = AlbumAgentModel::create($data);
  1634. if($res){
  1635. $d = [
  1636. 'code' =>0,
  1637. 'msg' =>'success',
  1638. ];
  1639. }else{
  1640. $d = [
  1641. 'code' =>1,
  1642. 'msg' =>'error',
  1643. ];
  1644. }
  1645. return $this->api($d);
  1646. }
  1647. /**
  1648. * @api {get} /api/album/get-agent-address 获取经销商地址(get-agent-address)
  1649. * @apiDescription 获取经销商地址(get-agent-address)
  1650. * @apiGroup Album
  1651. * @apiPermission none
  1652. * @apiVersion 0.1.0
  1653. * @apiSuccessExample {json} Success-Response:
  1654. * HTTP/1.1 200 OK
  1655. * {
  1656. * "status": true,
  1657. * "status_code": 0,
  1658. * "message": "",
  1659. * "data":[
  1660. * "realname":
  1661. * "phone":
  1662. * "address":
  1663. * ]
  1664. * }
  1665. * @apiErrorExample {json} Error-Response:
  1666. * HTTP/1.1 400 Bad Request
  1667. * {
  1668. * "state": false,
  1669. * "code": 1000,
  1670. * "message": "传入参数不正确",
  1671. * "data": null or []
  1672. * }
  1673. * 可能出现的错误代码:
  1674. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1675. */
  1676. public function albumGetAgentAdress(Request $request)
  1677. {
  1678. $userAuth = Auth('api')->user();
  1679. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1680. $validator = Validator::make($request->all(), [
  1681. 'store_id' => 'required',
  1682. ],[
  1683. 'store_id.required'=>'缺少商户参数',
  1684. ]);
  1685. if ($validator->fails()) {
  1686. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1687. }
  1688. $data = $request->input();
  1689. if($userAuth->is_dealer == 1){
  1690. $user_agent = AlbumAgentModel::where([['store_id',$data['store_id']],['user_id',$userAuth->id]])->first(['address','phone','realname']);
  1691. }else{
  1692. $user_agent = null;
  1693. }
  1694. return $this->api(compact('user_agent'));
  1695. }
  1696. /**
  1697. * @api {get} /api/album/get-banner 获取banner和視頻(get-banner)
  1698. * @apiDescription 获取banner和視頻(get-banner)
  1699. * @apiGroup Album
  1700. * @apiPermission none
  1701. * @apiVersion 0.1.0
  1702. * @apiSuccessExample {json} Success-Response:
  1703. * HTTP/1.1 200 OK
  1704. * {
  1705. * "status": true,
  1706. * "status_code": 0,
  1707. * "message": "",
  1708. * "data":[
  1709. * "video":{
  1710. * 'url':22222,
  1711. * 'name':22222,
  1712. * 'type':22222,
  1713. * 'agent_id':22222,
  1714. * }
  1715. * "banner":{
  1716. * 'url':22222,
  1717. * 'name':22222,
  1718. * 'type':22222,
  1719. * 'agent_id':22222,
  1720. * },
  1721. * ]
  1722. * }
  1723. * @apiErrorExample {json} Error-Response:
  1724. * HTTP/1.1 400 Bad Request
  1725. * {
  1726. * "state": false,
  1727. * "code": 1000,
  1728. * "message": "传入参数不正确",
  1729. * "data": null or []
  1730. * }
  1731. * 可能出现的错误代码:
  1732. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1733. */
  1734. public function albumGetBanner(Request $request)
  1735. {
  1736. $userAuth = Auth('api')->user();
  1737. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1738. if($userAuth->up_agent_id == 0){
  1739. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1740. $agent_id = $user_agent['id'];
  1741. }else{
  1742. $agent_id = $userAuth->up_agent_id;
  1743. }
  1744. $banner = AgentBannerModel::where([['agent_id',$agent_id],['type',0]])->get();
  1745. $video = AgentBannerModel::where([['agent_id',$agent_id],['type',1]])->first();
  1746. return $this->api(compact('banner','video'));
  1747. }
  1748. /**
  1749. * @api {get} /api/album/get-customer 获取客户信息(get-customer)
  1750. * @apiDescription 获取客户信息(get-customer)
  1751. * @apiGroup Album
  1752. * @apiPermission none
  1753. * @apiVersion 0.1.0
  1754. * @apiParam {int} [store_id] 商户id
  1755. * @apiParam {string} [openid_id] open_id
  1756. * @apiSuccessExample {json} Success-Response:
  1757. * HTTP/1.1 200 OK
  1758. * {
  1759. * "status": true,
  1760. * "status_code": 0,
  1761. * "message": "",
  1762. * "data":[
  1763. * "purpose_level":
  1764. * "comment":
  1765. * "address":
  1766. * "phone":
  1767. * "tips":
  1768. * ]
  1769. * }
  1770. * @apiErrorExample {json} Error-Response:
  1771. * HTTP/1.1 400 Bad Request
  1772. * {
  1773. * "state": false,
  1774. * "code": 1000,
  1775. * "message": "传入参数不正确",
  1776. * "data": null or []
  1777. * }
  1778. * 可能出现的错误代码:
  1779. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1780. */
  1781. public function albumGetCustomer(Request $request)
  1782. {
  1783. $userAuth = Auth('api')->user();
  1784. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1785. $validator = Validator::make($request->all(), [
  1786. 'store_id' => 'required',
  1787. 'open_id'=>'required'
  1788. ],[
  1789. 'store_id.required'=>'缺少商户参数',
  1790. 'open_id.required'=>'缺少用户参数',
  1791. ]);
  1792. if ($validator->fails()) {
  1793. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1794. }
  1795. $store_id = $request->input('store_id');
  1796. $open_id = $request->input('open_id');
  1797. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  1798. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1799. $customer = CustomerDetailsModel::where([['store_id',$store_id],['open_id',$open_id],['agent_id',$user_agent->id]])->first(['purpose_level','comment','tips','address']);
  1800. if(!empty($customer)){
  1801. $user = AlbumUserModel::where([['store_id',$store_id],['open_id',$open_id]])->first();
  1802. $customer->phone = $user->phone;
  1803. }
  1804. return $this->api(compact('customer'));
  1805. }
  1806. /**
  1807. * @api {post} /api/album/set-customer 设置客户信息(set-customer)
  1808. * @apiDescription 设置客户信息(set-customer)
  1809. * @apiGroup Album
  1810. * @apiPermission none
  1811. * @apiVersion 0.1.0
  1812. * @apiParam {int} [store_id] 商户id
  1813. * @apiParam {string} [openid_id] openid
  1814. * @apiParam {int} [level] 意向等级 1 25% 2 50% 3 75% 4 100%
  1815. * @apiParam {string} [comment] 备注
  1816. * @apiParam {string} [address] 地址
  1817. * @apiParam {string} [tips] 标签
  1818. * @apiSuccessExample {json} Success-Response:
  1819. * HTTP/1.1 200 OK
  1820. * {
  1821. * "status": true,
  1822. * "status_code": 0,
  1823. * "message": "",
  1824. * "data":[
  1825. * "purpose_level":
  1826. * "address":
  1827. * "comment":
  1828. * "phone":
  1829. * "tips":
  1830. * ]
  1831. * }
  1832. * @apiErrorExample {json} Error-Response:
  1833. * HTTP/1.1 400 Bad Request
  1834. * {
  1835. * "state": false,
  1836. * "code": 1000,
  1837. * "message": "传入参数不正确",
  1838. * "data": null or []
  1839. * }
  1840. * 可能出现的错误代码:
  1841. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1842. */
  1843. public function albumSetCustomer(Request $request)
  1844. {
  1845. $userAuth = Auth('api')->user();
  1846. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1847. $validator = Validator::make($request->all(), [
  1848. 'store_id' => 'required',
  1849. 'purpose_level' => 'required',
  1850. 'comment' => 'required',
  1851. 'address' => 'required',
  1852. 'tips' => 'required',
  1853. 'open_id'=>'required'
  1854. ],[
  1855. 'store_id.required'=>'缺少商户参数',
  1856. 'address.required'=>'缺少地址参数',
  1857. 'purpose_level.required'=>'缺少意向等级参数',
  1858. 'comment.required'=>'缺少客户备注参数',
  1859. 'tips.required'=>'缺少标签参数',
  1860. 'open_id.required'=>'缺少用户参数',
  1861. ]);
  1862. if ($validator->fails()) {
  1863. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1864. }
  1865. $store_id = $request->input('store_id');
  1866. $open_id = $request->input('open_id');
  1867. $add = $request->input();
  1868. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  1869. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1870. $customer = CustomerDetailsModel::where([['store_id',$store_id],['open_id',$open_id],['agent_id',$user_agent->id]])->first(['purpose_level','comment','tips']);
  1871. $add['agent_id'] = $user_agent->id;
  1872. $user = CustomerDetailsModel::create($add);
  1873. $customer->phone = $user->phone;
  1874. return $this->api([],0,'success');
  1875. }
  1876. /**
  1877. * @api {get} /api/album/customer-goods 获取客户浏览商品统计(customer-goods)
  1878. * @apiDescription 获取客户浏览商品统计(customer-goods)
  1879. * @apiGroup Album
  1880. * @apiPermission none
  1881. * @apiVersion 0.1.0
  1882. * @apiSuccessExample {json} Success-Response:
  1883. * HTTP/1.1 200 OK
  1884. * {
  1885. * "status": true,
  1886. * "status_code": 0,
  1887. * "message": "",
  1888. * "data":[
  1889. * {
  1890. * "name":
  1891. * "count":
  1892. * },
  1893. * ]
  1894. * }
  1895. * @apiErrorExample {json} Error-Response:
  1896. * HTTP/1.1 400 Bad Request
  1897. * {
  1898. * "state": false,
  1899. * "code": 1000,
  1900. * "message": "传入参数不正确",
  1901. * "data": null or []
  1902. * }
  1903. * 可能出现的错误代码:
  1904. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1905. */
  1906. public function albumCustomerGoods(Request $request)
  1907. {
  1908. $userAuth = Auth('api')->user();
  1909. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1910. $validator = Validator::make($request->all(), [
  1911. 'store_id' => 'required',
  1912. ],[
  1913. 'store_id.required'=>'缺少商户参数',
  1914. ]);
  1915. if ($validator->fails()) {
  1916. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1917. }
  1918. $store_id = $request->input('store_id');
  1919. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  1920. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1921. if($user_agent) $res= AlbumWatchRecord::where([['action',3],['store_id',$store_id],['agent_id',$user_agent->id]])->groupBy('detail')->get('detail');
  1922. $arr = array();
  1923. foreach ($res as $key=>$val){
  1924. $detail = json_decode($val['detail'],true);
  1925. //dd($detail);die;
  1926. $goods_id = $detail['goods_id'];
  1927. $goods = AlbumProductModel::where([['id',$goods_id]])->first();
  1928. if($goods){
  1929. $count= AlbumWatchRecord::where([['action',3],['store_id',$store_id],['detail',$detail]])->count();
  1930. $arr[] = ['name'=>$goods->name,'count'=>$count];
  1931. }
  1932. }
  1933. return $this->api($arr,0,'success');
  1934. }
  1935. /**
  1936. * @api {get} /api/album/customer-goods-detail 获取客户浏览商品详细(customer-goods-detail)
  1937. * @apiDescription 获取客户浏览商品详细(customer-goods-detail)
  1938. * @apiGroup Album
  1939. * @apiPermission none
  1940. * @apiVersion 0.1.0
  1941. * @apiSuccessExample {json} Success-Response:
  1942. * HTTP/1.1 200 OK
  1943. * {
  1944. * "status": true,
  1945. * "status_code": 0,
  1946. * "message": "",
  1947. * "data":[
  1948. * {
  1949. * "name":
  1950. * "time":
  1951. * "style":
  1952. * "cat":
  1953. * },
  1954. * ]
  1955. * }
  1956. * @apiErrorExample {json} Error-Response:
  1957. * HTTP/1.1 400 Bad Request
  1958. * {
  1959. * "state": false,
  1960. * "code": 1000,
  1961. * "message": "传入参数不正确",
  1962. * "data": null or []
  1963. * }
  1964. * 可能出现的错误代码:
  1965. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  1966. */
  1967. public function albumCustomerGoodsDetail(Request $request)
  1968. {
  1969. $userAuth = Auth('api')->user();
  1970. if(!$userAuth) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  1971. $validator = Validator::make($request->all(), [
  1972. 'store_id' => 'required',
  1973. 'open_id'=>'required'
  1974. ],[
  1975. 'store_id.required'=>'缺少商户参数',
  1976. 'open_id.required'=>'缺少用户参数',
  1977. ]);
  1978. if ($validator->fails()) {
  1979. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  1980. }
  1981. $store_id = $request->input('store_id');
  1982. $open_id = $request->input('open_id');
  1983. if($userAuth->is_dealer!=1) return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '该用户不是经销商!', $validator->messages());
  1984. $user_agent = AlbumAgentModel::where('user_id',$userAuth->id)->first();
  1985. if($user_agent) $res= AlbumWatchRecord::where([['action',3],['store_id',$store_id],['open_id',$open_id],['agent_id',$user_agent->id]])->get('detail','created_at')->toArray();
  1986. $arr = array();
  1987. foreach ($res as $key=>$val){
  1988. $detail = json_decode($val['detail'],true);
  1989. //dd($detail);die;
  1990. $goods_id = $detail['goods_id'];
  1991. $goods = AlbumProductModel::where([['id',$goods_id]])->first();
  1992. if($goods){
  1993. $style = AlbumProductStyleModel::where([['store_id',$store_id],['id',$goods->style]])->first();
  1994. $cat = AlbumCatModel::where([['store_id',$store_id],['id',$goods->cat_id]])->first();
  1995. $arr[] = ['name'=>$goods->name,'time'=>date('Y-m-d-h',strtotime($val['created_at'])),'style'=>$style->name,'cat'=>$cat->name];
  1996. }
  1997. }
  1998. return $this->api($arr,0,'success');
  1999. }
  2000. }