Beispiel #1
0
 /**
  * 获取token
  * @param null
  * @return mixed
  * @author zhengqian.zhu
  */
 public function getToken(Request $request)
 {
     $request = $request->only('appid', 't', 'sign');
     $validator = \Validator::make($request, array('appid' => 'required', 't' => 'required', 'sign' => 'required'));
     if ($validator->fails()) {
         return RestHelp::parametersIllegal($validator->messages()->first());
     }
     $app = App::where("app_id", $request['appid'])->first();
     if (!$app) {
         return RestHelp::encodeResult("21001", "appId not found");
     }
     $appsecret = $app->app_secret;
     $md5Sign = $this->md5Str($request['appid'], $appsecret, $request['t']);
     if ($request['sign'] != $md5Sign) {
         return RestHelp::encodeResult("21002", "sign not correct");
     }
     $day = new \DateTime();
     $day->modify("+2 hours");
     $app->expire_at = $day->format("Y-m-d H:i:s");
     //如果存在未过期的token,直接返回token
     if (!$app->access_token || $app->expire_at <= date("Y-m-d H:i:s")) {
         $app->access_token = Uuid::v4(false);
     }
     $app->save();
     return RestHelp::success(['access_token' => $app->access_token, 'expires_in' => 7200]);
 }
Beispiel #2
0
 /**
  * 重新支付
  * @return string
  * @author zhengqian@dajiayao.cc
  */
 public function rePay()
 {
     $buyerId = $this->buyerId;
     $wxUser = Buyer::find($buyerId)->wxUser;
     if (!$wxUser) {
         return RestHelp::encodeResult(24000, "user illegality");
     }
     $inputData = $this->inputData->all();
     $validator = Validator::make($inputData, ['orderNumber' => 'required']);
     if ($validator->fails()) {
         return RestHelp::parametersIllegal($validator->messages()->first());
     }
     $objOrder = Order::where('order_number', $inputData['orderNumber'])->first();
     $objShop = Shop::find($objOrder->shop_id);
     if (!$objShop) {
         return RestHelp::encodeResult(21000, sprintf("shop short id %s not found in db", $objOrder->shop_id));
     }
     //查找价格
     $itemTotal = $objOrder->item_total;
     $grandTotal = $objOrder->grand_total;
     $payment = new Payment();
     $payment->serial_number = OrderHelper::getPaymentSerialNumber(1);
     $payment->payment_number = '';
     $payment->order_id = $objOrder->id;
     $payment->order_number = $inputData['orderNumber'];
     $payment->buyer_id = $buyerId;
     $payment->amount = $itemTotal;
     $payment->channel = Payment::PAYMENT_CHANNEL_PXX;
     $payment->type = Payment::PAYMENT_TYPE_WX;
     $payment->status = Order::PAY_STATUS_NO;
     $payment->save();
     $subject = '';
     //32
     $body = '';
     //128
     foreach ($objOrder->orderItems as $orderItem) {
         $subject .= $orderItem->items->title . "*" . $orderItem->items->spec . "*" . $orderItem->quantity . ",";
         $body .= $orderItem->items->title . $orderItem->items->spec . $orderItem->quantity;
     }
     try {
         \Pingpp\Pingpp::setApiKey('sk_live_3dKEivmziedjzitFhaHL7gYF');
         $ch = \Pingpp\Charge::create(array('order_no' => $payment->serial_number, 'app' => array('id' => 'app_XTOW5SXTWLGCGKef'), 'channel' => 'wx_pub', 'amount' => $grandTotal * 100, 'client_ip' => $this->inputData->ip(), 'currency' => 'cny', 'subject' => mb_substr($subject, 0, 32), 'body' => mb_substr($body, 0, 128), 'extra' => array('open_id' => $wxUser->openid)));
     } catch (\Exception $e) {
         return RestHelp::encodeResult(22003, $e->getMessage(), ['orderNum' => $inputData['orderNumber']]);
     }
     //写入paytmentLOg
     $paymentLog = new PaymentLog();
     $paymentLog->payment_id = $payment->id;
     $paymentLog->channel = Payment::PAYMENT_CHANNEL_PXX;
     $paymentLog->request_data = $ch;
     $paymentLog->respond_data = '';
     $paymentLog->save();
     //保存支付流水号
     $payment->payment_number = json_decode($ch)->id;
     $payment->save();
     return RestHelp::success(['orderNumber' => $inputData['orderNumber'], 'paymentNumber' => $payment->serial_number, 'charge' => json_decode($ch, true)]);
 }
Beispiel #3
0
 /**
  * 微信公众账号添加
  * @return string
  */
 public function create(MpService $mpService)
 {
     $resquetData = json_decode($this->inputData, true);
     $validator = Validator::make($resquetData, ['name' => 'required', 'appid' => 'required', 'appsecret' => 'required', 'mp_id' => 'required', 'comment' => '']);
     if ($validator->fails()) {
         return RestHelp::parametersIllegal($validator->messages()->first());
     }
     $id = $mpService->create(new Mp($resquetData['name'], $resquetData['mp_id'], $resquetData['appid'], $resquetData['appsecret'], $resquetData['comment']));
     return RestHelp::success(['wx_id' => $id]);
 }
 /**
  * @return string
  * @author zhengqian@dajiayao.cc
  */
 public function pageStatistics()
 {
     $inputData = $this->request->only(['page_id', 'begin_date', 'end_date']);
     $validator = Validator::make($inputData, ['page_id' => 'required|integer', 'begin_date' => 'required|integer', 'end_date' => 'required|integer']);
     if ($validator->fails()) {
         return RestHelp::parametersIllegal($validator->messages()->first());
     }
     $appid = Session::get('wx_appid');
     $appsecret = Session::get('wx_appsecret');
     try {
         $ret = $this->statisticService->pageStatistic($inputData['page_id'], $inputData['begin_date'], $inputData['end_date'], $appid, $appsecret);
     } catch (\Exception $e) {
         return RestHelp::encodeResult($e->getCode(), $e->getMessage());
     }
     return RestHelp::success($ret);
 }
Beispiel #5
0
 /**
  * 收藏与取消收藏
  * @param $id
  * @return string
  * @author zhengqian@dajiayao.cc
  */
 public function favorite()
 {
     $inputdata = $this->inputData->only('shopShortId', 'flag');
     $validator = \Validator::make($inputdata, ['shopShortId' => 'required', 'flag' => 'required|boolean']);
     if ($validator->fails()) {
         return RestHelp::parametersIllegal($validator->messages()->first());
     }
     $objShop = Shop::getShopByShort($inputdata['shopShortId']);
     if (!$objShop) {
         return RestHelp::encodeResult(21001, "shop not found in db");
     }
     $buyerId = is_null($this->buyerId) ? 0 : $this->buyerId;
     if ($inputdata['flag'] == false) {
         FavoriteShop::where('buyer_id', $buyerId)->where('shop_id', $objShop->id)->delete();
     } elseif ($inputdata['flag'] == true) {
         $favorite = new FavoriteShop();
         $favorite->buyer_id = $buyerId;
         $favorite->shop_id = $objShop->id;
         $favorite->save();
     } else {
         return RestHelp::encodeResult(21001, 'flag is only 0 or 1');
     }
     return RestHelp::success();
 }
Beispiel #6
0
 /**
  * 生成订单
  * @return string
  * @author zhengqian@dajiayao.cc
  */
 public function create()
 {
     //        $inputData = $this->inputData->all();
     $buyerId = $this->buyerId;
     $wxUser = Buyer::find($buyerId)->wxUser;
     if (!$wxUser) {
         return RestHelp::encodeResult(24000, "user illegality");
     }
     $inputData = json_decode(file_get_contents("php://input"), true);
     $validator = Validator::make($inputData, ['shopShortId' => 'required', 'items' => 'required', 'deliverAddressId' => 'required', 'paymentType' => 'required', "shopType" => '', 'orderNumber' => '', "isAnonymous" => 'required|Boolean']);
     if ($validator->fails()) {
         return RestHelp::parametersIllegal($validator->messages()->first());
     }
     if (!isset($inputData['items']) or !is_array($inputData['items'])) {
         return RestHelp::encodeResult(23003, 'item must be array');
     }
     $objShop = Shop::getShopByShort($inputData['shopShortId']);
     if (!$objShop) {
         return RestHelp::encodeResult(21000, sprintf("shop short id %s not found in db", $inputData['shopShortId']));
     }
     $deliver = BuyerAddress::find($inputData['deliverAddressId']);
     if (!$deliver) {
         return RestHelp::encodeResult(23001, sprintf("deliver address id: %s not found in db", $inputData['deliverAddressId']));
     }
     if (!isset($inputData['orderNumber'])) {
         $orderNumber = OrderHelper::getOrderSerialNumber();
     } else {
         $orderNumber = $inputData['orderNumber'];
     }
     $deliverAddressId = $inputData['deliverAddressId'];
     $shopId = $objShop->id;
     $itemTotal = 0;
     $postageFlag = 0;
     $shelfItems = $objShop->getItemsOnShelf();
     //检查商品合法性、库存等等
     foreach ($inputData['items'] as $item) {
         if (!array_key_exists('id', $item) or !array_key_exists('count', $item)) {
             return RestHelp::encodeResult(23005, "items not correct");
         }
         $objItem = Item::find($item['id']);
         $itemTotal += $objItem->price * $item['count'];
         if ($objItem->postage_type == Item::POSTAGE_TYPE_BUYER) {
             $postageFlag++;
         }
         if ($objItem->sale_status == Item::SALE_STATUS_NO) {
             return RestHelp::encodeResult(23006, sprintf("%s已停售", $objItem->title));
         }
         if ($objItem->shelf_status == Item::SHELF_STATUS_NO or !array_key_exists($objItem->id, $shelfItems)) {
             return RestHelp::encodeResult(23006, sprintf("%s已下架", $objItem->title));
         }
         //库存
         if ($objItem->stock < $item['count']) {
             return RestHelp::encodeResult(23006, sprintf("%s库存不足", $objItem->title));
         }
     }
     $postage = $postageFlag ? $this->settingService->getSettingByKey(Setting::KEY_ORDER_POSTAGE)->value : 0;
     $grandTotal = $itemTotal + $postage;
     $sessionTotal = WxUserKv::getValue(Buyer::find($this->buyerId)->wxUser->id, WxUserKv::BUYER_CHECK_PRICE);
     if ((string) $sessionTotal != (string) $grandTotal) {
         return RestHelp::encodeResult(22002, "illegal operation");
     }
     $discount = 0;
     $orderType = Order::PAYMENT_TYPE_WX;
     //如果传入了订单号
     if (isset($inputData['orderNumber'])) {
         //update
         $objOrder = Order::where('order_number', $inputData['orderNumber'])->first();
         if (!$objOrder) {
             return RestHelp::encodeResult(22001, "the order not found");
         }
         //new
         try {
             $orderId = $this->orderService->update($objOrder, $itemTotal, $grandTotal, $discount, $grandTotal - $discount, $inputData['isAnonymous'] ? 1 : 0, $postage, $orderType, $deliverAddressId);
         } catch (\Exception $e) {
             return RestHelp::encodeResult(23004, $e->getMessage());
         }
     } else {
         //new
         try {
             $orderId = $this->orderService->create($orderNumber, $shopId, $this->buyerId, $itemTotal, $grandTotal, $discount, $grandTotal - $discount, $postage, $orderType, $deliverAddressId, $inputData['isAnonymous'] ? 1 : 0, null, null);
         } catch (\Exception $e) {
             return RestHelp::encodeResult(23004, $e->getMessage());
         }
         //减少库存
         $commissonTotal = 0;
         foreach ($inputData['items'] as $item) {
             $objItem = Item::find($item['id']);
             $objItem->stock -= $item['count'];
             $objItem->save();
             //计算挨个佣金
             $commissonTotal += $objItem->commission * $item['count'];
         }
         //生成佣金表数据
         $commisson = OrderCommission::firstOrNew(['order_id' => $orderId]);
         $commisson->amount = $objShop->is_direct_sale == 'Y' ? 0 : $commissonTotal;
         $commisson->status = OrderCommission::STATUS_UNCONFIRMED;
         $commisson->save();
         //生成seller commisson
         $sellerCommission = SellerCommission::firstOrNew(['order_id' => $orderId, 'seller_id' => $objShop->seller_id]);
         $sellerCommission->amount = $objShop->is_direct_sale == 'Y' ? 0 : $commissonTotal;
         $sellerCommission->status = SellerCommission::STATUS_UNCONFIRMED;
         $sellerCommission->save();
     }
     $subject = '';
     //32
     $body = '';
     //128
     $itemIdList = \DB::table('order_items')->where('order_id', $orderId)->lists('item_id');
     foreach ($inputData['items'] as $item) {
         if (isset($inputData['orderNumber'])) {
             $orderItem = OrderItem::where('order_id', $orderId)->where('item_id', $item['id'])->first();
             $quantity = $orderItem ? $orderItem->quantity : 0;
             $count = $item['count'];
             $balance = $count - $quantity;
             $objItem = Item::find($item['id']);
             $objItem->stock -= $balance;
             $objItem->save();
             OrderItem::where('order_id', $orderId)->where('item_id', $item['id'])->forceDelete();
             //如果支付失败,返回修改删除了某个商品,则恢复库存 Step 1  @author zhengqian
             foreach ($itemIdList as $k => $itid) {
                 if ($item['id'] == $itid) {
                     unset($itemIdList[$k]);
                 }
             }
         }
         $orderItem = new OrderItem();
         $orderItem->order_id = $orderId;
         $orderItem->item_id = $item['id'];
         $objItem = Item::find($item['id']);
         $orderItem->name = $objItem->name;
         $subject .= $objItem->title . "*" . $objItem->spec . "*" . $objItem->{$item}['count'] . ",";
         $body .= $objItem->title . $objItem->spec . $objItem->{$item}['count'];
         $orderItem->title = $objItem->title;
         $orderItem->code = $objItem->code;
         $orderItem->barcode = $objItem->barcode;
         $orderItem->type = ItemType::find($objItem->type_id)->name;
         $orderItem->quantity = $item['count'];
         $orderItem->price = $objItem->price;
         $orderItem->item_total = $objItem->price * $item['count'];
         $commissionsRate = $this->settingService->getSettingByKey(Setting::KEY_COMMISSIONS_RATE);
         if (!$commissionsRate) {
             $commissionsRate = Setting::DEFAULT_KEY_COMMISSIONS_RATE;
         } else {
             $commissionsRate = $commissionsRate->value;
         }
         $orderItem->commission = $orderItem->item_total * $commissionsRate;
         $orderItem->save();
     }
     //如果支付失败,返回修改删除了某个商品,则恢复库存 Step 2 @author zhengqian
     if (isset($inputData['orderNumber'])) {
         foreach ($itemIdList as $itid) {
             $orderItem = OrderItem::where('order_id', $orderId)->where('item_id', $itid)->first();
             $quantity = $orderItem->quantity;
             $objItem = Item::find($itid);
             $objItem->stock += $quantity;
             $objItem->save();
             OrderItem::where('order_id', $orderId)->where('item_id', $itid)->forceDelete();
         }
     }
     $payment = new Payment();
     $payment->serial_number = OrderHelper::getPaymentSerialNumber(1);
     $payment->payment_number = '';
     $payment->order_id = $orderId;
     $payment->order_number = Order::find($orderId)->order_number;
     $payment->buyer_id = $this->buyerId;
     $payment->amount = $itemTotal;
     $payment->channel = Payment::PAYMENT_CHANNEL_PXX;
     $payment->type = Payment::PAYMENT_TYPE_WX;
     $payment->status = Order::PAY_STATUS_NO;
     $payment->save();
     try {
         \Pingpp\Pingpp::setApiKey('sk_live_3dKEivmziedjzitFhaHL7gYF');
         $ch = \Pingpp\Charge::create(array('order_no' => $payment->serial_number, 'app' => array('id' => 'app_XTOW5SXTWLGCGKef'), 'channel' => 'wx_pub', 'amount' => $sessionTotal * 100, 'client_ip' => $this->inputData->ip(), 'currency' => 'cny', 'subject' => mb_substr($subject, 0, 32), 'body' => mb_substr($body, 0, 128), 'extra' => array('open_id' => $wxUser->openid)));
     } catch (\Exception $e) {
         return RestHelp::encodeResult(22003, $e->getMessage(), ['orderNum' => $orderNumber]);
     }
     //写入paytmentLOg
     $paymentLog = new PaymentLog();
     $paymentLog->payment_id = $payment->id;
     $paymentLog->channel = Payment::PAYMENT_CHANNEL_PXX;
     $paymentLog->request_data = $ch;
     $paymentLog->respond_data = '';
     $paymentLog->save();
     //保存支付流水号
     $payment->payment_number = json_decode($ch)->id;
     $payment->save();
     return RestHelp::success(['orderNumber' => $orderNumber, 'paymentNumber' => $payment->serial_number, 'charge' => json_decode($ch, true)]);
 }
 /**
  * @param $id
  * @return string
  * @author zhengqian@dajiayao.cc
  */
 public function update($id)
 {
     if (!$id) {
         return RestHelp::parametersIllegal("id is required");
     }
     $buyerAddress = BuyerAddress::find($id);
     if (!$buyerAddress) {
         return RestHelp::encodeResult(23000, 'deliver_addr is not found');
     }
     $buyerId = $this->buyerId;
     $inputData = $this->inputData->all();
     $validator = Validator::make($inputData, ['countyId' => '', 'address' => '', 'postcode' => '', 'receiver' => '', 'mobile' => '', 'default' => '']);
     if ($validator->fails()) {
         return RestHelp::parametersIllegal($validator->messages()->first());
     }
     $this->buyerAddressService->update($buyerAddress, $buyerId, isset($inputData['countyId']) ? $inputData['countyId'] : $buyerAddress->county_id, isset($inputData['address']) ? $inputData['address'] : $buyerAddress->address, isset($inputData['postcode']) ? $inputData['postcode'] : $buyerAddress->postcode, isset($inputData['receiver']) ? $inputData['receiver'] : $buyerAddress->receiver, isset($inputData['mobile']) ? $inputData['mobile'] : $buyerAddress->mobile, isset($inputData['default']) ? $inputData['default'] : $buyerAddress->default);
     return RestHelp::success();
 }
 /**
  * 更新设备的地理位置
  * @param DeviceService $deviceService
  * @param $sn
  * @return string
  * @author zhengqian@dajiayao.cc
  */
 public function setLocation(DeviceService $deviceService, $sn)
 {
     if (!$sn) {
         return RestHelp::parametersIllegal('sn is required');
     }
     $dev = Device::getDeviceBySn($sn);
     if (!$dev) {
         return RestHelp::encodeResult(24001, sprintf("sn:%s not found", $sn));
     }
     $inputData = json_decode($this->inputData, true);
     $validator = Validator::make($inputData, ['longitude' => 'required', 'latitude' => 'required', 'address' => 'required', 'location' => 'required']);
     if ($validator->fails()) {
         return RestHelp::parametersIllegal($validator->messages()->first());
     }
     $dev->longitude = $inputData['longitude'];
     $dev->latitude = $inputData['latitude'];
     $dev->address = $inputData['address'];
     $dev->position = $inputData['position'];
     $dev->save();
     return RestHelp::success();
 }
 public function countyIndex()
 {
     $cityId = $this->inputData->get('cityId');
     if (!$cityId) {
         return RestHelp::parametersIllegal("city_id is required");
     }
     $county = Address::where('parent_id', $cityId)->get();
     $arrRet = array();
     foreach ($county as $k => $p) {
         $arrRet[$k]['id'] = $p->id;
         $arrRet[$k]['name'] = $p->address;
     }
     return RestHelp::success($arrRet);
 }
Beispiel #10
0
 /**
  * 页面具体信息
  * @param $pageId
  * @return string
  * @author zhengqian@dajiayao.cc
  */
 public function getInfo($pageId)
 {
     if (!$pageId) {
         return RestHelp::parametersIllegal("page id is required");
     }
     $objPage = WeixinPage::find($pageId);
     if (!$objPage) {
         return RestHelp::encodeResult(23001, sprintf("page id is %s not found", $pageId));
     }
     $retPage = ['title' => $objPage->title, 'description' => $objPage->description, 'icon_url' => $objPage->icon_url, 'url' => $objPage->url, 'comment' => $objPage->comment];
     $retDevice = array();
     $devicePage = DevicePage::where('wx_page_id', $pageId)->get();
     foreach ($devicePage as $dp) {
         $device = $dp->device->weixinDevice;
         foreach ($device as $d) {
             array_push($retDevice, ['sn' => $d->sn, 'device_id' => $d->wx_device_id, 'comment' => $d->comment, 'status' => $d->status]);
         }
     }
     return RestHelp::success(['page' => $retPage, 'device' => $retDevice]);
 }
Beispiel #11
0
 /**
  * 店铺详情
  * @param $id
  * @return string
  * @author zhengqian@dajiayao.cc
  */
 public function detail($short_id)
 {
     if (!$short_id) {
         return RestHelp::parametersIllegal("shop id is requird");
     }
     $objShop = Shop::where('short_id', $short_id)->first();
     if (!$objShop) {
         return RestHelp::encodeResult(21000, "shop is not found in db");
     }
     $buyerId = $this->buyerId;
     $favShop = FavoriteShop::where("shop_id", $objShop->id)->where("buyer_id", $buyerId)->first();
     $favorite = $favShop ? 1 : 0;
     $seller = $objShop->seller;
     $arrSeller = array();
     $arrSeller['id'] = $seller->id;
     $arrSeller['name'] = $seller->wxUser ? $seller->wxUser->nickname : $seller->realname;
     $arrSeller['mobile'] = $seller->mobile;
     $banner = "";
     $arrShop = array();
     $arrShop['shortId'] = $objShop->short_id;
     $arrShop['name'] = $objShop->subtitle;
     $arrShop['banner'] = $banner;
     $arrShop['ad'] = "/1.png";
     $arrShop['avatar'] = $objShop->thumbnail;
     $arrShop['type'] = $objShop->type;
     $arrShop['banner'] = $objShop->banner ? ImageUtil::getRuleImgSize($objShop->banner, 750, 246) : '';
     $arrShop['region']['provinceId'] = $objShop->province_id;
     $arrShop['region']['cityId'] = $objShop->city_id;
     $arrShop['region']['countyId'] = $objShop->county_id;
     $items = array_values($objShop->getItemsOnShelf());
     $arrItem = array();
     foreach ($items as $k => $item) {
         $arrItem[$k]['id'] = $item->id;
         $arrItem[$k]['title'] = $item->title;
         $arrItem[$k]['name'] = $item->name;
         $objImage = $item->image->first();
         $imagUrl = $objImage ? ImageUtil::getRuleImgSize($objImage->url, 260, 260) : "";
         $arrItem[$k]['image'] = $imagUrl;
         $arrItem[$k]['code'] = $item->code;
         $arrItem[$k]['supplier'] = $item->supplier->title;
         $arrItem[$k]['spec'] = $item->spec;
         $arrItem[$k]['weight'] = $item->weight;
         $arrItem[$k]['volume'] = $item->volume;
         $arrItem[$k]['price'] = $item->price;
         $arrItem[$k]['marketPrice'] = $item->market_price;
         //总计售出
         $sellsCount = $this->redis->get("dajiayao:mall:item:sellcount:" . $item->id);
         $arrItem[$k]['sales'] = $sellsCount ? (int) $sellsCount : 0;
         $arrItem[$k]['comment'] = $item->comment;
         //以前购买人数
         $arrRedisBuyers = $this->redis->smembers("dajiayao:mall:item:buyers:" . $item->id);
         $arrBuyers = array();
         foreach ($arrRedisBuyers as $b => $buyers) {
             if ($b < 4) {
                 //最多五个
                 array_push($arrBuyers, json_decode($buyers));
             }
         }
         $arrItem[$k]['buyers'] = $arrBuyers;
     }
     //TODO 支付方式
     $arrPayment = ['alipay'];
     //TODO 广告位,推广
     $arrPromotions = [["title" => "星巴克", "link" => "#", "image" => ImageUtil::getRuleImgSize("/shopimages/starbucks_promotion.jpg", 1176, 210)]];
     return RestHelp::success(['favorite' => $favorite, 'shop' => $arrShop, 'promotions' => $arrPromotions, 'owner' => $arrSeller, "availablePayments" => $arrPayment, 'items' => $arrItem, 'visitorCount' => rand(100000, 105000)]);
 }